在python

时间:2015-12-25 15:43:22

标签: python-2.7 loops multidimensional-array mean

我必须执行一个移动平均线。在下面的代码中,输入文件(stress1.txt)包含两列x和y值。每个y介于0.9x和1.1x之间需要进行平均。遍及这两个列表的代码的最后一部分是正确的,因为它在我打印出来时为每个上限和下限返回正确的x值。但是,平均值没有正确完成,我已经尝试了所有可能的平均命令放置。我真的被困住了,因为我看起来对我来说似乎是正确的。另外,我对编程很新,所以我的代码可能不是非常pythonic。有人能指出出了什么问题吗?

import sys,string
import numpy as np
from math import *
import fileinput

infiles = ['stress1.txt']

oldlist = []
xlist = []
newlist = [0]

IN = fileinput.input(infiles)

for step in range(21): ## number of rows in stress1.txt
    line = IN.readline()
    [t,a] = string.split(line)
    time = float(t)
    acf = float(a)
    oldline = [time,acf]
    oldlist.append(oldline) ## nested list containing x and y values
for i in range(len(oldlist)):
    t11 = float(0.9*oldlist[i][0])
    t1 = float("{0:.4f}".format(t11))
    t22 = float(1.1*oldlist[i][0])
    t2 = float("{0:.4f}".format(t22))
    xlist.append(t1)
    xlist.append(t2)
xlist = [xlist[i:i+2] for i in range(0, len(xlist), 2)] ## nested list containing upper and lower bounds for each x. This list has the same size as 'oldlist'.
for i in range(len(oldlist)):
    for j in range(len(xlist)):
        if (xlist[i][0] <= oldlist[j][0] < xlist[i][1]):
            #print oldlist[j][0]
            newlist.append(oldlist[j][0])
    #print '\n'
    mean = sum(newlist)/float(len(newlist)) ## not giving the right average
    print mean

我已将我的问题编辑为包含stress1.txt:

0       63.97308696
0.005   62.68978803
0.01    58.95890345
0.015   53.11671683
0.02    45.64732412
0.025   37.10669444
0.03    28.05011931
0.035   18.98414178
0.04    10.34110231
0.045   2.470985737
0.05    -4.356736338
0.055   -9.947472597
0.06    -14.17532845
0.065   -16.97779073
0.07    -18.35134411
0.075   -18.34723586
0.08    -17.0675793
0.085   -14.66065262
0.09    -11.3157742
0.095   -7.257500157
0.1     -2.7383312

代码应该平均每个'块',如下所示。初始块仅包含单个值,因此它本身就是平均值。后面的块有多个值,必须进行平均和输出。{抱歉使这个线程如此冗长}

0.005


0.01


0.015


0.02


0.025


0.03


0.035


0.04


0.045


0.045
0.05


0.05
0.055
0.06


0.055
0.06
0.065


0.06
0.065
0.07


0.065
0.07
0.075


0.07
0.075
0.08


0.075
0.08
0.085


0.08
0.085
0.09


0.085
0.09
0.095


0.09
0.095
0.1


0.09
0.095
0.1

1 个答案:

答案 0 :(得分:3)

如果我正确理解了你的目标,你需要在每次迭代开始时使用一个空的newlist []。如果你没有把它清空,那就充满了以前的街区&#39;值。

for i in range(len(oldlist)):

    #Make it empty
    newlist = []

    for j in range(len(xlist)):
        if (xlist[i][0] <= oldlist[j][0] and  oldlist[j][0] <= xlist[i][1]):
            newlist.append(oldlist[j][0])
    print(repr(newlist))
    mean = sum(newlist)/float(len(newlist))
    print(mean)

产生输出:

[0.0]
0.0
[0.005]
0.005
[0.01]
0.01
[0.015]
0.015
[0.02]
0.02
[0.025]
0.025
[0.03]
0.03
[0.035]
0.035
[0.04]
0.04
[0.045]
0.045
[0.045, 0.05, 0.055]
0.04999999999999999
[0.05, 0.055, 0.06]
0.055
[0.055, 0.06, 0.065]
0.06
[0.06, 0.065, 0.07]
0.065
[0.065, 0.07, 0.075]
0.07
[0.07, 0.075, 0.08]
0.07500000000000001
[0.075, 0.08, 0.085]
0.08
[0.08, 0.085, 0.09]
0.085
[0.085, 0.09, 0.095]
0.09000000000000001
[0.09, 0.095, 0.1]
0.09500000000000001
[0.09, 0.095, 0.1]
0.09500000000000001