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