运行 平均问题

Running average issue

如果您能就这个问题给我任何提示,我将不胜感激。我该如何开始,知道吗?

我是运行一个产生能量的软件。我有 N 个时间步(块),并且在每个时间步中它生成几行。第一列是每个时间步中生成的行数的计数器。因此,我将有一个如下所示的文本文件,它是一个巨大文件的简化示例:

#Block:           3           3           0
           1        -0.3746468365E+04         0.9420348015E+00        -0.3745526330E+04         0.1636348407-151       316.8679         0.2626532250-312
           2        -0.3746707738E+04         0.1149418891E+01        -0.3745558319E+04         0.1220432713E+00       386.6247         0.2626532250-312
           3        -0.3746757823E+04         0.1195378991E+01        -0.3745562444E+04         0.1636348407-151       402.0841         0.2626532250-312
#Block:           4           4           1
           1        -0.3746625102E+04         0.6261182789E+00        -0.3745998983E+04         0.1636348407-151       210.6045         0.2626532250-312
           2        -0.3746723956E+04         0.7190568481E+00        -0.3746004899E+04         0.1636348407-151       241.8658         0.2626532250-312
           3        -0.3746758909E+04         0.7514702248E+00        -0.3746007439E+04         0.1636348407-151       252.7685         0.2626532250-312
           4        -0.3746707738E+04         0.7067911904E+00        -0.3746000947E+04        -0.3205844292E+00       237.7401         0.2626532250-312
           5        -0.3746680579E+04         0.6897161187E+00        -0.3745990863E+04         0.1636348407-151       231.9966         0.2626532250-312

#Block:           5           5           1
           1        -0.3746625102E+04         0.6261182789E+00        -0.3745998983E+04         0.1636348407-151       210.6045         0.2626532250-312
           2        -0.3746723956E+04         0.7190568481E+00        -0.3746004899E+04         0.1636348407-151       241.8658         0.2626532250-312
           3        -0.3746758909E+04         0.7514702248E+00        -0.3746007439E+04         0.1636348407-151       252.7685         0.2626532250-312
           4        -0.3746707738E+04         0.7067911904E+00        -0.3746000947E+04        -0.3205844292E+00       237.7401         0.2626532250-312
           5        -0.3746680579E+04         0.6897161187E+00        -0.3745990863E+04         0.1636348407-151       231.9966         0.2626532250-312

我想计算这些数据第 3 列的 运行 平均值并保存在 "average text file" 中。因此,

这是我试过的方法

with open('xxx.txt','r') as file:

 groups = [] # Will contain the final data

 current_group = [] # Temporary
 line = file.readline()
 while line != "":
    if line.startswith("#Block"):
        # Store the current group and start a new one
        groups.append(current_group)
        current_group = []
    else:
        # Add the number to the current group
        current_group.append(float(line.split()[2]))
    line = file.readline()
    averages = list() 
    for i in xrange(len(groups)):
      flatten_list = list(itertools.chain(*groups[:i+1]))
      print (flatten_list)
      averages.append(sum(flatten_list) / len(flatten_list))
    with open('output.txt', 'a+') as output_f:
      output_f.writelines(averages)

到目前为止你做得很好,因为你已经设法将每个块的第 3 列提取为 groups 列表中的列表,现在,在你的 while 循环之后你可以做

averages = list() 
for i in range(len(groups)):
    flatten_list = list(itertools.chain(*groups[:i+1]))
    print flatten_list
    averages.append(sum(flatten_list) / len(flatten_list))
with open('output.txt', 'a+') as output_f:
    output_f.writelines(averages)

完整代码将是

with open('xxx.txt','r') as file:

 groups = [] # Will contain the final data

 current_group = [] # Temporary
 line = file.readline()
 while line != "":
    if line.startswith("#Block"):
        # Store the current group and start a new one
        groups.append(current_group)
        current_group = []
    else:
        # Add the number to the current group
        current_group.append(float(line.split()[2]))
    line = file.readline()
    averages = list() 
 for i in range(len(groups)):
   flatten_list = list(itertools.chain(*groups[:i+1]))
   print (flatten_list)
   averages.append(sum(flatten_list) / len(flatten_list))
 with open('output.txt', 'a+') as output_f:
   output_f.writelines(averages)

输入文件名是'input.txt'。 输出文件名为 'result.txt'.

with open('INPUT.TXT', 'r') as fout:
  lst_of_num=[]
  for line in fout:

    if line[0]=='#':
      #print line
      if lst_of_num:
        #print sum(lst_of_num) / float(len(lst_of_num))
        with open("result.txt", "a+") as myfile:
          myfile.write(str(sum(lst_of_num) / float(len(lst_of_num)))+'\n')
        #lst_of_num=[]
    else:
      lst_of_num.append(float(line.split()[2]))

  with open("result.txt", "a+") as myfile:
        myfile.write(str(sum(lst_of_num) / float(len(lst_of_num)))+'\n')

更新为 Python 3 和新输入

import itertools

history = []

with open('sample.txt') as f:
    group = []
    for line in f:
        line = line.strip()
        if 'Block' in line:
            if group:
                history.append(group)
                group = []
        elif line:
            group.append(float(line.split()[2]))

    history.append(group)

with open('output.txt', 'w') as f:
    for i in range(len(history)):
        l = list(itertools.chain(*history[:i+1]))
        print(sum(l) / len(l), file=f)

并输出:

$ cat output.txt 
1.0956108945
0.84749816805
0.790241385023077