Python 如何根据另一列中的符号变化计算列表中的数学差异
我有一个大列表,如下所示:Python 如何根据另一列中的符号变化计算列表中的数学差异,python,numpy,Python,Numpy,我有一个大列表,如下所示: 515.20320783 0.05658324 516.20360241 0.03486690 517.20399699 0.02094333 518.20439157 0.02199200 519.20478615 0.03610312 520.20518073 0.05240170 521.20557530 0.05834678 522.20596988 0.04893
515.20320783 0.05658324
516.20360241 0.03486690
517.20399699 0.02094333
518.20439157 0.02199200
519.20478615 0.03610312
520.20518073 0.05240170
521.20557530 0.05834678
522.20596988 0.04893050
523.20636446 0.02992211
524.20675904 0.01215849
525.20715362 0.00224414
526.20754820 -0.00168170
527.20794277 -0.00608658
528.20833735 -0.01532678
529.20873193 -0.02889651
530.20912651 -0.04376070
531.20952109 -0.05776619
532.20991567 -0.07066846
533.21031024 -0.08255477
534.21070482 -0.09145077
535.21109940 -0.09284463
536.21149398 -0.08325564
我首先需要根据第2列的值的符号变化来分隔第1列的两个列表,这意味着第1列中的所有值(其在第2列中的对应值为正值)都应该在一个列表中,而第2列的值在另一个列表中为负值。然后,我想计算子列表(之前创建的列表)的第一项和最后一项之间的差异。我所做的工作如下,但似乎不对
import numpy as np
l = [f for f in sorted(os.listdir('.')) if f.startswith('config')]
maxnum = np.max([int(os.path.splitext(f)[0].split('_')[1]) for f in l])
l = ['configuration_%d.out' % i for i in range(maxnum)]
meand = []
meands=[]
for i, d in enumerate (CASES):
a = np.loadtxt(os.path.join(root, directory,d)).T
y = a[1]
x = a[0]
mig = np.abs(a[5])
theta = a[3]
s = a[2]
h = min(y)+6
t = max(y)-6
meand = []
for i in range(1,len(y)):
if h <y[i]<t :
idx = np.where(np.diff(np.sign(theta)) != 0)[0] + 1
meand_length = s[idx]-s[idx-1]
meand.append(meande_length)
meands.append(np.mean(meand))
print(meands)
将numpy导入为np
l=[f表示排序中的f(os.listdir('.'),如果f.startswith('config')]
maxnum=np.max([int(os.path.splitext(f)[0].split(“”“)[1]),用于l中的f])
l=['配置\%d.out'%i用于范围内的i(maxnum)]
平均值=[]
平均数=[]
就我而言,列举(案例):
a=np.loadtxt(os.path.join(root,directory,d)).T
y=a[1]
x=a[0]
mig=np.abs(a[5])
θ=a[3]
s=a[2]
h=最小值(y)+6
t=最大值(y)-6
平均值=[]
对于范围(1,len(y))中的i:
如果h使用numpy
方法符号和非零
:
list1=np.arange(0,20,2)
#数组([0,2,4,6,8,10,12,14,16,18]
list2=np.linspace(-1,1,10)
#~array([-1,-0.77,-0.55,-0.33,-0.11,0.11,0.33,0.55,0.77,1.]))
s=np.符号(列表2)
#数组([-1.、-1.、-1.、-1.、-1.、-1,1,1,1,1,1.]))
idx符号变化=np.非零(np.差异!=0)[0]
#数组([4])
拆分列表=np.split(列表1,idx符号更改)
#[数组([0,2,4,6]),数组([8,10,12,14,16,18])]
diff=[l[-1]-l[0]表示拆分列表中的l]
# [6, 10]
平均值差异=np.平均值(差异)
请注意,如果有多个符号变化,这也会起作用。你的想法是正确的,但我需要这样做:每个列表的第一项和最后一项之间的差异意味着list1_a:6-0
和list1_b:18-8
我的意思是,不需要有两个不同的列表,我需要计算6-0
然后18-8
然后我需要计算这些差异的平均值。