python - 获取多个列表之间每列数据的平均值

标签 python python-2.7 list

我正在尝试创建数据基线。我需要从每个列表中获取每一列的平均值,有十个列表。每个列表大约有 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 中列的平均值,每个文件都是一个列表。

最佳答案

内置函数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)
>>>

关于python - 获取多个列表之间每列数据的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41880048/

相关文章:

Python3 - 如何从现有抽象类定义抽象子类?

python - 导入错误: No module named manage Django

python - 尽可能快地在 python 中导入大的 tecplot block 文件

python - 是否可以使用 python 在 Dialogflow 中触发意图?

Python即使满足条件也无法退出while循环

python - 在本地 gpu 和 colab gpu 上进行分布式训练

c++ - 在 C++ 中使用查找函数时遇到问题

取消列表中某个值以下的元素

python - python 中的多重继承以及向继承类传递参数

c++ - 关于 std::list,如果我想按降序排序,我可以简单地调用 "mylist.sort(>)"吗?