I have been able to import 3 scores (or numbers) for each person in my CSV file and have it work out the average of the three scores for each person but what I need to do next is sort the list of averages shown in the output from high to low. I have searched everywhere and have received 'Float' errors with everything I try.
from collections import defaultdict, deque
with open("highscores.csv", "r+")as file:
file.seek(0)
scores = file.readlines()
user_scores = defaultdict(lambda:deque(maxlen=3))
for line in scores:
name, score = line.rstrip('\n').split(',')
score = int(score)
user_scores[name].append(score)
for name in user_scores:
avg = (user_scores[name])
x = sorted(avg)
avg2 = sum(x) / float(len(x))
print(name, avg2)
Output:
Odis 22.0
Vance 20.0
John 26.0
Marinda 25.0
Shenita 18.0
Marquitta 24.0
Concepcion 17.0
Robby 23.0
Valda 19.0
Ernesto 21.0
Jack 5.0
My CSV file looks like this :
Jack 4
Jack 5
Jack 6
Concepcion 7
Shenita 8
Valda 9
Vance 10
Ernesto 11
Odis 12
Robby 13
Marquitta 14
Marinda 15
John 16
Concepcion 17
Shenita 18
Valda 19
Vance 20
Ernesto 21
Odis 22
Robby 23
Marquitta 24
Marinda 25
John 26
Concepcion 27
Shenita 28
Valda 29
Vance 30
Ernesto 31
Odis 32
Robby 33
Marquitta 34
Marinda 35
John 36