输入若干学生数据,包括姓名,以及ds,db,c 三门课程的成绩,存放到序列中,然后按总成绩由高到低输出所有学生的信息,使用内置函数sorted及lambda表达式实现。
时间: 2024-01-05 11:02:11 浏览: 208
好的,以下是代码实现:
```python
# 定义学生数据
students = [
{'name': 'Tom', 'ds': 85, 'db': 90, 'c': 75},
{'name': 'Jerry', 'ds': 80, 'db': 85, 'c': 95},
{'name': 'Alice', 'ds': 90, 'db': 80, 'c': 85},
{'name': 'Bob', 'ds': 70, 'db': 80, 'c': 90},
]
# 使用sorted及lambda表达式按总成绩排序
students_sorted = sorted(students, key=lambda x: x['ds']+x['db']+x['c'], reverse=True)
# 输出所有学生信息
for student in students_sorted:
print(f"Name: {student['name']}, Score: {student['ds']}, {student['db']}, {student['c']}, Total: {student['ds']+student['db']+student['c']}")
```
输出结果:
```
Name: Jerry, Score: 80, 85, 95, Total: 260
Name: Alice, Score: 90, 80, 85, Total: 255
Name: Tom, Score: 85, 90, 75, Total: 250
Name: Bob, Score: 70, 80, 90, Total: 240
```
阅读全文