I have some data which is a list of scores for each of various users. The following code finds the maximum value of the last three scores for the user (sorted so the user with the lowest score is shown first), and print that out:
dlist = {c:max(d[c][-3:]) for c in d} #max for each user from last 3
itmlist = sorted(dlist.items(), key=lambda x: x[1]) #sort items
for z in itmlist:
print('{}: {} '.format(z[0], z[1])) #print user-by-user
I'm trying to modify this to use sum(l)/len(l)
in order to find the average value of the last three scores for each user, and then sort this to print the lowest user averages in order, but have hit a dead end.
Can anyone point me in the right direction?
--
EDIT:
This is the code used to generate the list. I am using a text file containing data containing scores in a format like:
Bob:2
John:7
This is then read using this:
[view post history]
The question is a bit unclear, So I have made some assumptions, tell me if I am wrong somewhere?
d = {"bob":[1,2,3,4], "anmol":[5,4,3,2,1], "jhon":[3,2,1,8,7]}
new_sorted_list = sorted(d.keys(), key=lambda x: sum(d[x][-3:])/3)
print new_sorted_list
>>> ['anmol', 'bob', 'jhon']
for record in new_sorted_list:
print record+" : "+str(sum(d[record][-3:])/3.0)
>>> anmol : 2.0
bob : 3.0
jhon : 5.33333333333