【问题标题】:Moving Average plot移动平均线图
【发布时间】:2016-04-13 16:18:41
【问题描述】:

我有一个巨大的文本文件,类似于下面链接中的表格。每个 xxx 框的行数不同,但列数相同:

Here is the link to figure

  • 图中第一个点是表中前3个值的平均值,

  • 第二个是前5个值的平均值,

  • 第三个是前8个值的平均值,

  • 等等……

我无法编写适用于整个文本文件的通用 python 代码。 你能帮我解决这个问题吗?

我尝试了这段代码,但出现错误:

【问题讨论】:

  • 什么错误?在哪里?依据什么数据?
  • 你得到什么错误?这些数字 3、5、8 是从哪里来的?平均多少个值的规则是什么?
  • 3,5,8没有明显的“等等”
  • 是斐波那契吗? :-)
  • 表格实际上是一个文本文件。每个 XXXX 是一个产生多个能量值的循环,我想将平均值绘制为循环数的函数。

标签: python moving-average


【解决方案1】:

我相信您正在寻找的总体布局是:

with open('file','r') as file:
    groups = [] # Will contain the final data
    current_group = [] # Temporary

    line = file.readline()
    while line != "":
        if line == "XXXX":
            # 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(int(line.split()[2]))
        line = file.readline()

【讨论】:

  • current_group.append(int(line.split()[2])) : IndexError: 列表索引超出范围
  • @AlirezaBahrami 表示该行少于3个项目(以空格分隔)。您需要修改该行以执行任何适当的数据解析。我不能帮助你,因为我不知道输入文件的细节。
  • 另外,我只是想展示如何遍历添加到组的行,然后在每次跨越边界时开始一个新组。
猜你喜欢
  • 2017-09-01
  • 2013-12-22
  • 1970-01-01
  • 2022-01-26
  • 2011-06-29
  • 2019-04-24
  • 2012-05-24
  • 2021-01-18
  • 1970-01-01
相关资源
最近更新 更多