python-2.7 - python 中嵌套循环中的平均值

标签 python-2.7 loops multidimensional-array mean

我必须执行运行平均值。在下面的代码中,输入文件 (stress1.txt) 包含两列 x 和 y 值。 0.9x 到 1.1x 之间的每个 y 都需要进行平均。遍历两个列表的代码的最后一部分是正确的,因为当我打印出来时,它返回每个上限和下限的正确 x 值。然而,平均没有正确完成,我已经尝试了mean命令的所有可能的位置。我真的很困惑,因为我所拥有的在我看来在逻辑上是正确的。另外,我对编程还很陌生,所以我的代码可能不太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

代码预计会对每个“ block ”进行平均,如下所示。初始 block 仅包含单个值,因此其本身就是平均值。后面的 block 有多个值,必须对其进行平均并输出。{抱歉让这个线程变得如此冗长}

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

最佳答案

如果我正确理解你的目标,那么你在每次迭代开始时都需要一个空的 newlist[] 。如果不清空它,它就会充满之前 block 的值。

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

关于python-2.7 - python 中嵌套循环中的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34464180/

相关文章:

python - python中的分组列表元素

arrays - 如何[最好]比较多类型[但仍然受控]数组中的值?

python - 从 pandas 数据框中的多行中提取非 nan 值

python - 循环 'for element in list_[a:b]' 是否使用解释器优化的未使用副本?

python - 如何使用 Python 循环遍历列中的行并对其进行计数?

java - 我写了两段代码,几乎一模一样,但其中一段运行速度比另一段快得多(Java)

java - 为什么我的 ImageButtons 不能为我的滑动益智游戏随机播放?

php - 多维数组中的求和值

python 没有名为 ujson 的模块,虽然它已经安装

python-2.7 - 如何通过 REST API 将文件上传到我自己的 Google 云端硬盘