I have been trying to create a program which the teacher log in to view the scores of a test of a school class. The scores are stored in a text file like this:
Charlotte:7
Charlotte:4
Charlotte:3
Chelsea:2
Chelsea:9
Chelsea:5
Jeff:1
Jeff:10
As you can see there are multiple scores for each student, I need to be able to sort these scores by highest score out of the class with each student's highest score. I've worked out how to sort it by each student's highest score with the following code but I now need to sort those highest scores of the students into the highest to lowest scored in the class.
import operator
import collections
d = collections.defaultdict(lambda: collections.deque(maxlen=3))
with open("class {0}.txt".format(Class)) as f:
#these lines adds the text file into the dictionary
for line in f:
name,score = line.strip().split(":")
d[name].append(score)
#The next lines gets each students highest score
for k in d:
high = max(d[k])
k + " " + " ".join(map(str, high))
The error is in the following lines, where i've tried to sort the list of 'high scores' into highest to lowest
print(sorted(high, reverse=True))
It currently prints out this:
['7']
I have worked out how to sort it by each student's highest score but I am stuck on how to sort each highest score into highest to lowest.
Could someone give me some pointers or help on how I could sort the list and print it out like this..
Jeff 10
Chelsea 9
Charlotte 7
Thanks!