1

我必须计算特定嵌套列表中每一列的平均值,然后将平均值保存到新列表中。到目前为止,在我的代码中,我已将原始列表设置为嵌套列表并将其转置以在列中读取。我只是不确定如何编写平均值。

#First open the data text file
import re

f = open('C:\Python27\Fake1.txt', 'r')

#Convert to a nested list
nestedlist = []
q = f.read()
f.close()
numbers = re.split('\n', q) #Splits the \n and \t out of the list
newlist = []
for row in numbers:
   newlist.append(row.split('\t'))


#Reading in columns
def mytranspose(nestedlist):
   list_prime = []
   for i in range(len(nestedlist[0])):
          list_prime.append([])
   for row in nestedlist:
          for i in range(len(row)):
                 list_prime[i].append(row[i])
   return(list_prime)
print (mytranspose(newlist))


#Average of Columns
def myaverage(nestedlist):
   avg_list = []
   a = 0
   avg = 0
   for i in newlist:
          a = sum(newlist[i])
          avg = a/len(row)
          avg_list.append(avg[i])
   return(list_prime)

print(myaverage(newlist))
4

2 回答 2

10

这是一个更简单的方法来完成整个事情:

with open('C:\Python27\Fake1.txt', 'r') as f:
    data = [map(float, line.split()) for line in f]

num_rows = len(data)
num_cols = len(data[0])

totals = num_cols * [0.0]
for line in data:
    for index in xrange(num_cols):
        totals[index] += line[index]

averages = [total / num_rows for total in totals]
print averages

但是,我建议将numpy用于此类事情,因为它变得微不足道(并且速度更快):

import numpy as np
data = np.loadtxt('C:\Python27\Fake1.txt')
print data.mean(0)
于 2012-06-28T19:18:23.203 回答
7

假设您有列表列表

table = [[1, 2, 3],  [10, 20, 30], [100, 200, 300]]

您可以使用 zip 转置它并将原始列表列表作为参数列表传递(星号的作用):

transposed = zip(*table)
: [(1, 10, 100), (2, 20, 200), (3, 30, 300)]

要获得这些列的总和,您可以使用映射函数映射每个条目:

sums = map(sum, transposed)
: [111, 222, 333]

由于平均值是总和除以长度,我们可以用一个函数来做到这一点:

def avg(items):
    return float(sum(items)) / len(items)

或者您可以在 lambda 中执行此操作:

avg = lambda items: float(sum(items)) / len(items)

并用它代替 sum:

averages = map(avg, transposed)

您可以将所有这些放在一个函数中,如下所示:

table = [[1, 2, 3],  [10, 20, 30], [100, 200, 300]]
averages = map(lambda items: float(sum(items)) / len(items), zip(*table))

但这有点难以理解,所以分解它通常更清楚:

table = [[1, 2, 3],  [10, 20, 30], [100, 200, 300]]
transposed = zip(*table)
avg = lambda items: float(sum(items)) / len(items)
averages = map(avg, transposed)
于 2012-06-28T19:24:09.610 回答