我将给出一些演示代码:
# -*- coding: utf-8 -*-
import csv
from collections import defaultdict
from statistics import mean
class_file = 'scores.csv'
open_file = open(class_file)
csv_file = csv.reader(open_file)
def main():
# First, use student name to group by all scores, this will
# generate structure like this:
# {
# 'Andrew': [1, 2, 3, 4, 5]),
# 'Luck': [10, 20]),
# }
score_groups = defaultdict(list)
for name, score in csv_file:
score_groups[name].append(int(score))
# Secondary, use the 3 latest socres only
l3_score_groups = [(key, value[-3:]) for key, value in score_groups.items()]
print('1. Alphabetical order with each students highest score.')
l3_highest_score_groups = [(key, max(values)) for key, valu