MST

星途 面试题库

面试题:Python字典在简单数据统计中的应用

假设有一个列表,其中包含多个字典,每个字典代表一个学生的信息,格式为 {'name': '学生姓名', 'score': 成绩}。请编写一个Python函数,统计不同成绩区间(如0 - 59, 60 - 79, 80 - 100)的学生人数,并以字典形式返回结果。
11.9万 热度难度
编程语言Python

知识考点

AI 面试

面试题答案

一键面试
def count_score_ranges(students):
    result = {
        '0 - 59': 0,
        '60 - 79': 0,
        '80 - 100': 0
    }
    for student in students:
        score = student['score']
        if 0 <= score <= 59:
            result['0 - 59'] += 1
        elif 60 <= score <= 79:
            result['60 - 79'] += 1
        elif 80 <= score <= 100:
            result['80 - 100'] += 1
    return result