I am trying to sort averages of scores in a class by pupil, from highest to lowest. My data is stored in text files like this:
Charlie:0
Seema:2
Amber:4
Paige:5
Amber:8
Keith:1
Charlie:8
Seema:0
Charlie:9
Seema:3
Paige:0
Paige:4
Paige:4
Charlie:1
Keith:5
Keith:3
Here is my code so far:
with open("class {0}.txt".format(Class)) as f:
d = {}
#loop to split the data in the text file
for line in f:
column = line.split(":")
#identifies the key and value with either 0 or 1
names = column[0][Click Here](null)
scores = int(column[1].strip())
#appends values if a key already exists
tries = 0
while tries < 3:
d.setdefault(names, []).append(scores)
tries = tries + 1
for names, v in sorted(d.items()):
average = (sum(v)/len(v))
print(names,average)
averages=[]
averages.append(average)
I have worked out the averages however, I'm stuck on how I could sort these averages by highest to lowest, here is what ive tried
list = sorted(averages, key=lambda tup: tup[1], reverse=True)
However, it gives the error..
TypeError: 'float' object is not subscriptable
Im quite new to Python amd i am not sure what i am doing wrong, so any pointers of help would be much appreciated, Thank you in advance!