我必须计算特定嵌套列表中每列的平均值,然后将平均值保存到新列表中。到目前为止,在我的代码中,我已将原始列表设置为嵌套列表并将其转换为读取列。我只是不确定如何编码平均值。
#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))
答案 0 :(得分: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)
答案 1 :(得分:6)
假设您有列表清单
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)
用这个代替总和:
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)