我已经定义了一个名为CalcspeedAngle
的函数def CalcspeedAngle(k):
xpos = [1,2,3,4,5,6,7,8,9]
ypos = [10,11,12,13,14,15,16,17,18]
turnrates = [entry/3.099 for entry in xpos][::k]
vts = [entry/3.099 for entry in ypos][::k]
return(turnrates,vts)
然后我写
turnrates, vts = CalcspeedAngle(k)
我的问题是:我如何创建两个新的列表(m1list和m2list),其中包含平均值的转折和vts,其中k = 1,2,3,4,5,6,7,8,9,10 ,15,20?换句话说,我该如何创建:
m1list = [mean(flattened(turnrates(k=1))), mean(flattened(turnrates(k=2))), ...]
m2list = [mean(flattened(vts(k=1))), mean(vts(turnrates(k=2))), ...]
非常感谢任何帮助。
答案 0 :(得分:1)
简化代码的一种方法是对range(10)
执行一次迭代,解压缩2个列表列表,计算每个列表的平均值,并将它们附加到结果列表中:
def lists_of_means():
m1list = []
m2list = []
for i in range(10):
turnrates, vts = CalcspeedAngle(i)
m1list.append(np.mean(turnrates))
m2list.append(np.mean(vts))
return m1list, m2list