下面是一个示例代码,可以实现保留每个组的最高单场比赛。
# 假设每个组的比赛成绩保存在一个字典中,字典的键为组名,值为该组的比赛得分列表
scores = {
'Group A': [85, 92, 78, 90],
'Group B': [76, 88, 94, 82],
'Group C': [89, 90, 95, 88]
}
# 创建一个新的字典来保存每个组的最高比赛得分
highest_scores = {}
# 遍历每个组的得分列表,找到最高得分,并将其保存到新字典中
for group, score_list in scores.items():
highest_score = max(score_list)
highest_scores[group] = highest_score
# 打印每个组的最高得分
for group, highest_score in highest_scores.items():
print(f"{group}: {highest_score}")
输出结果将会是:
Group A: 92
Group B: 94
Group C: 95
这个示例代码首先定义了一个包含每个组比赛得分的字典。然后,通过遍历每个组的得分列表,使用max()
函数找到最高得分,并将其保存到一个新的字典中。最后,使用循环打印出每个组的最高得分。