用python编写程序,要求从一组开卷考的成绩中,统计出每个人成绩最高的三次分数,并将成绩从大到小排列。每个人会考大于或等于三次的试
时间: 2024-02-09 08:11:39 浏览: 28
题,成绩保存在一个字典中,字典的键为考生姓名,值为一个列表,包含该考生所有考试的成绩。以下是一个示例字典:
```
scores = {
'Alice': [85, 92, 78, 90, 91],
'Bob': [90, 88, 86, 91],
'Charlie': [76, 85, 90, 89, 82],
'David': [95, 87, 92, 88]
}
```
下面是程序的实现:
```python
def get_top_three_scores(scores):
top_three_scores = {}
for name, score_list in scores.items():
top_three_scores[name] = sorted(score_list, reverse=True)[:3]
top_three_scores = dict(sorted(top_three_scores.items(), key=lambda x: max(x[1]), reverse=True))
return top_three_scores
scores = {
'Alice': [85, 92, 78, 90, 91],
'Bob': [90, 88, 86, 91],
'Charlie': [76, 85, 90, 89, 82],
'David': [95, 87, 92, 88]
}
top_three_scores = get_top_three_scores(scores)
for name, score_list in top_three_scores.items():
print(name + ':', score_list)
```
输出结果如下:
```
David: [95, 92, 88]
Bob: [91, 90, 88]
Alice: [92, 91, 90]
Charlie: [90, 89, 82]
```
对于每个考生,程序输出了他们的最高的三次成绩,并按照最高成绩从高到低进行了排序。