【问题标题】:Get the mean of each column of data between multiple lists获取多个列表之间每列数据的平均值
【发布时间】:2017-01-26 18:05:05
【问题描述】:

我正在尝试为数据创建基线。我需要从每个列表中获取每列的平均值,并且有十个列表。每个列表有大约 50 个元素。通过每列取平均值会给我在道路上那个点的平均值,所以我需要小心不要取列表的平均值。我可以通过仅在文件名循环中建立索引来单独获取每一列,但这非常低效。然后我会使用 MatplotLib 绘制数据,但这部分应该很容易。这是我到目前为止的代码:

def graphWriterIRI():
    n = 0
    for filename in os.listdir(os.getcwd()):
    # Initialize a new set of lists for each file
        startList = []
        endList = []
        iriRList = []
        iriLList = []
        # Load the file
        if re.search('BASELINE',filename):
            with open(filename, 'rU') as file:
                for row in csv.DictReader(file):
                    try:
                        startList.append(float(row['Start-Mi']))
                        endList.append(float(row['  End-Mi']))
                    except:
                        startList.append(float(row['Start-MP']))
                        endList.append(float(row['  End-MP']))
                    try:
                        iriRList.append(float(row[' IRI R e']))
                        iriLList.append(float(row['IRI LWP ']))
                    except:
                        iriRList.append(float(row[' IRI RWP']))
                        iriLList.append(float(row['IRI LWP ']))

        print iriRList[0] # prints column[0] of the list but I need this for 50 rows and two lists.

这是我在代码中引入的一些数据:

Start-Mi      End-Mi      IRI LWP   IRI R e
  194.449   194.549          80.3      87.4
  194.549   194.649          85.3      91.1
  194.649   194.749          87.4      95.6
  194.749   194.849          83.6      72.5
  194.849   194.949          73.7      81
  194.949   195.049          85.2      87.2
  195.049   195.149          106.3    111.5
  195.149   195.249          84.2      92.4
  195.249   195.349          95.5     95.5
  195.349   195.449          60.1      67.2
  195.449   195.549          56.6     51.3
  195.549   195.649          80.6      74.4
  195.649   195.749          73.7      69.9
  195.749   195.849          49.6      48.1
  195.849   195.949          48.1      50.2
  195.949   196.049          53.3      45.2
  196.049   196.149          55.8      45.8
  196.149   196.249          46.7      48.1

我特别想做的是获取每个文件的 iriRList 和 iriLList 中列的平均值,每个文件都是一个列表。

【问题讨论】:

  • 您对每个文件都有一个iriRList,并且您想要每个文件中第 i 列的平均值?
  • 是的,我有一个 iriRList 和一个 iriLList,我需要对每个文件中的每个相应数据点求平均值。这是我上传到 github 的一些文本文件。 github.com/thomasawolff/verification_text_data。我正在使用基线数据集。
  • 所以我需要英里点 194.449 和 194.549 等文件之间所有数据点的平均值。
  • 但是每个文件都是一个列表,我们使用的列表是 iriRList 和 iriLList

标签: python python-2.7 list


【解决方案1】:

内置函数zip 将转置一个序列序列。您可以使用它为每列创建元组。我不确定你是如何组织所有数据的,但这是我的想法:

>>> one = [1,2,3,4]
>>> two = [2,3,4,5]
>>> three = [3,4,5,6]

>>> for column in zip(one, two, three):
    print(column, sum(column), sum(column) / 3.0)


((1, 2, 3), 6, 2.0)
((2, 3, 4), 9, 3.0)
((3, 4, 5), 12, 4.0)
((4, 5, 6), 15, 5.0)
>>>

如果您从每个文件中累积列表:

def graphWriterIRI():
    n = 0
    iRlists = []
    for filename in os.listdir(os.getcwd()):
        ...
        ...
        print iriRList[0]
        iRlists.append(iriRList)

你会这样使用它:

>>> for column in zip(*iRlists):
    print(column, sum(column), sum(column) / float(len(iRlists)))


((1, 2, 3), 6, 2.0)
((2, 3, 4), 9, 3.0)
((3, 4, 5), 12, 4.0)
((4, 5, 6), 15, 5.0)
>>>

【讨论】:

  • 嗯,我认为我遇到的一个问题是我无法区分一个列表和另一个列表。
  • 我收到了这个错误:TypeError: zip argument #1 must support iteration
  • 我喜欢你用这个去哪里
  • 我要参加一个会议,我会在大约 30 - 45 分钟后回来。谢谢!
  • @hollow_Victory - 你必须catch the TypeError 并检查它以查看它是什么 - 如果它是一个列表或一个元组或任何sequence 它应该可以工作。
猜你喜欢
  • 1970-01-01
  • 2015-10-25
  • 2023-02-15
  • 1970-01-01
  • 2017-10-12
  • 2016-12-09
  • 1970-01-01
  • 2015-08-01
  • 2019-09-13
相关资源
最近更新 更多