问题
I have an assignment to do. I found out how to execute what is required in my own way but the solution is only partial. It doesn't work for a nested list. These are my codes.
def calc_averages():
allprices =[ ['', '', '', '', 1.0, 2.0, 1.2, 1.3, 1.1, '', '', ''],
['', '', '', 1.2, 1.0, 2.0, 1.2, 1.3, 1.1, '', '', ''],
['', '', '', 1.2, '', 1.8, 1.3, 1.1, '', '', '', ''],
['', '', '', '', 1.0, 2.0, 1.2, 1.2, '', '', '', ''],
['', '', '', '', 1.0, 2.0, 1.1, 1.2, 1.4, 1.8, 1.9, 2.2] ]
averages = []
aList = []
for lst in allprices:
aList.append(lst[5])
averages.append(sum(aList)/len(aList))
return averages
This works fine for calculating the average of a single month which is the total of 6th value from each list divided by 5. However, when I try to calculate the average for all 12 months using the above code, it doesn't work unless I change those empty strings to 0s.
ef calc_averages():
allprices =[ [0, 0, 0, 0, 1.0, 2.0, 1.2, 1.3, 1.1, 0, 0, 0],
[0, 0, 0, 1.2, 1.0, 2.0, 1.2, 1.3, 1.1, 0, 0, 0],
[0, 0, 0, 1.2, 0, 1.8, 1.3, 1.1, 0, 0, 0, 0],
[0, 0, 0, 0, 1.0, 2.0, 1.2, 1.2, 0, 0, 0, 0],
[0, 0, 0, 0, 1.0, 2.0, 1.1, 1.2, 1.4, 1.8, 1.9, 2.2] ]
averages = []
aList = []
bList = []
cList = []
dList = []
eList = []
fList = []
gList = []
hList = []
iList = []
jList = []
kList = []
lList = []
for lst in allprices:
aList.append(lst[0])
bList.append(lst[1])
cList.append(lst[2])
dList.append(lst[3])
eList.append(lst[4])
fList.append(lst[5])
gList.append(lst[6])
hList.append(lst[7])
iList.append(lst[8])
jList.append(lst[9])
kList.append(lst[10])
lList.append(lst[11])
averages.append(sum(aList)/len(aList))
averages.append(sum(bList)/len(bList))
averages.append(sum(cList)/len(cList))
averages.append(sum(dList)/len(dList))
averages.append(sum(eList)/len(eList))
averages.append(sum(fList)/len(fList))
averages.append(sum(gList)/len(gList))
averages.append(sum(hList)/len(hList))
averages.append(sum(iList)/len(iList))
averages.append(sum(jList)/len(jList))
averages.append(sum(kList)/len(kList))
averages.append(sum(lList)/len(lList))
return averages
When I try to run it with those empty strings, it gives me this error; TypeError: unsupported operand type(s) for +: 'int' and 'str'
I am also aware that it is not written in a good way. If you have any suggestions for improvement please tell me. Thank you already :)
回答1:
Your issue is that you are trying to add a string and an int together.
Instead of sum(aList)/len(aList), I would do
sum(list(filter(lambda x: x != '', aList)))/ len(aList)
which removes all the empty characters out from the list.
To wrap everything together where I clean up you having multiple lists:
def main():
allprices =[['', '', '', '', 1.0, 2.0, 1.2, 1.3, 1.1, '', '', ''],
['', '', '', 1.2, 1.0, 2.0, 1.2, 1.3, 1.1, '', '', ''],
['', '', '', 1.2, '', 1.8, 1.3, 1.1, '', '', '', ''],
['', '', '', '', 1.0, 2.0, 1.2, 1.2, '', '', '', ''],
['', '', '', '', 1.0, 2.0, 1.1, 1.2, 1.4, 1.8, 1.9, 2.2]]
alllists = [[allprices[i][j] for i in range(len(allprices))] for j in range(12)]
averages = []
for alist in alllists:
averages.append(sum(list(filter(lambda x: x != '', alist)))/ len(alist))
print(averages)
main()
来源:https://stackoverflow.com/questions/35539493/my-code-works-for-a-single-list-but-not-for-a-nested-list-i-need-to-improve-it