Reputation: 13
I have recorded students' names and scores in an external file and now I want to display them sorted by highest average. But the problem is that a student can take 2, 3 and 4 times the quiz and have 4 different scores for example. Each detail is recorded in a new line, so the same student can have multiple records. How do you tell the system to only pick one time the name of the student but calculate his/her average score and display it?
This is what I get when I display the content of the file alphabetically.
Abidjan Danane scored 8
Abidjan Danane scores 9
Babell Zoro scored 8
Babell Zoro scored 8
Baby Toba scores 7
Baby Toba scored 9
Catarase Betty scored 8
Catarase Betty scored 9
scores = []
with open(ClassName) as f:
for line in f:
user_score = line.split() #splits line on each space
scores.append([int(user_score[3]), user_score[0]])
print(sorted(scores, reverse = True))
Abidjan Danane has an average score of 8.5
Babell Zoro has an average score of 8.0
Baby Toba has an average score of 8.0
Catarase Betty has an average score of 8.5
Upvotes: 1
Views: 44
Reputation: 46859
this is a simple option:
import re
from collections import defaultdict
from statistics import mean
name_scores = defaultdict(list)
with open(ClassName) as file:
for line in file:
# split the line at 'scores' or 'scored'
name, score = re.split(' score[sd] ', line)
name_scores[name].append(int(score))
# name_scores = defaultdict(<class 'list'>,
# {'Abidjan Danane': [8, 9], 'Babell Zoro': [8, 8],
# 'Baby Toba': [7, 9], 'Catarase Betty': [8, 9]})
for name, scores in sorted(name_scores.items()):
score = mean(scores)
print(f'{name:20s} {score:2.1f}')
Upvotes: 1