运行平均问题

时间:2016-04-14 07:38:24

标签: python python-3.x moving-average

如果你能对这个问题给我任何暗示,我一定会很感激。我怎么能开始呢,任何想法?

我正在运行一个产生能量的软件。我有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列的运行平均值,并保存在“平均文本文件”中。因此,

  • 文本文件中的第一个值应该是块1的第3列的平均值。 (前3个值=(3.5 + 7.1 + 5.4)/ 3)

  • 文本文件中的第二个值应该是块1和块2的第3列的平均值。 (前8个值=(3.5 + 7.1 + 5.4 + 2.5 + 1.1 + 5.4 + 4.4 + 1.4)/ 8)

  • 文本文件中的第三个值应该是块1的第3列和第2块以及第3块的平均值。 (前12个值=(3.5 + 7.1 + 5.4 + 2.5 + 1.1 + 5.4 + 4.4 + 1.4 + 3.5 + 1.1 + 8.1 + 9.4)/ 12)

  • 我有N个街区。实际上我正在计算所有块的第3列的平均值作为时间步长的函数。

这是我试过的

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 个答案:

答案 0 :(得分:0)

到目前为止,您已经成功了,因为您已经设法将每个块的第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)

答案 1 :(得分:0)

输入文件名是' 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')

答案 2 :(得分:0)

更新到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