下面是一个使用Python的示例代码,展示如何按不同的子类别显示测验的分数:
class Test:
def __init__(self, category, score):
self.category = category
self.score = score
tests = [
Test("Math", 90),
Test("Math", 85),
Test("Science", 92),
Test("Science", 88),
Test("English", 95),
Test("English", 80)
]
# 创建一个字典来存储每个子类别的分数列表
category_scores = {}
# 遍历测试列表
for test in tests:
# 如果子类别已经在字典中,则将分数添加到对应的列表中
if test.category in category_scores:
category_scores[test.category].append(test.score)
# 如果子类别不在字典中,则创建一个新的列表,并将分数添加到列表中
else:
category_scores[test.category] = [test.score]
# 打印每个子类别的分数列表
for category, scores in category_scores.items():
print(f"Category: {category}")
print(f"Scores: {scores}")
print()
运行此代码将输出:
Category: Math
Scores: [90, 85]
Category: Science
Scores: [92, 88]
Category: English
Scores: [95, 80]
这样,你就可以按照不同的子类别显示测验的分数了。
上一篇:按不同的主键列和聚合函数进行分组
下一篇:按不同方案设置身份验证组件。