Lets assume I have a (text) file with the following structure (name, score):
a 0
a 1
b 0
c 0
d 3
b 2
And so on. My aim is to sum the scores for every name and order them from highest score to lowest score. So in this case, I want the following output:
d 3
b 2
a 1
c 0
In advance I do not know what names will be in the file.
I was wondering if there is an efficient way to do this. My text file can contain up to 50,000 entries.
The only way I can think of is just start at line 1, remember that name and then go over the whole file to look for that name and sum. This looks horribly inefficient, so I was wondering if there is a better way to do this.
sorted(split)with the samekey(being the letters)dictwith the name as the key and the score as the value. That can be made slightly neater by usingdefaultdict(int).