MST

星途 面试题库

面试题:Python if语句在复杂数据结构处理中的应用

有一个字典数据结构如下:students = { 'Alice': {'age': 20,'scores': [85, 90, 78]}, 'Bob': {'age': 21,'scores': [70, 65, 80]}}。请使用if语句判断每个学生的平均成绩是否大于80,如果大于80,则将学生名字添加到一个新列表中并返回,用Python代码实现。
19.2万 热度难度
编程语言Python

知识考点

AI 面试

面试题答案

一键面试
students = { 'Alice': {'age': 20,'scores': [85, 90, 78]}, 'Bob': {'age': 21,'scores': [70, 65, 80]}}
new_list = []
for name, info in students.items():
    average_score = sum(info['scores']) / len(info['scores'])
    if average_score > 80:
        new_list.append(name)
print(new_list)