学生成绩管理按总成绩排序,平均分怎么跟着总成绩走,求源代码
时间: 2024-05-10 20:18:41 浏览: 16
以下是一个简单的Python代码示例,用于按总成绩排序和计算平均分。
```python
class Student:
def __init__(self, name, grades):
self.name = name
self.grades = grades
self.total = sum(grades)
self.average = self.total / len(grades)
students = [
Student('Alice', [90, 85, 95]),
Student('Bob', [75, 80, 85]),
Student('Charlie', [80, 90, 85])
]
# 按总成绩排序
students.sort(key=lambda x: x.total, reverse=True)
# 计算平均分
total_average = sum(student.total for student in students) / len(students)
# 输出结果
for student in students:
print(f'{student.name} - Total: {student.total}, Average: {student.average}')
print(f'Total Average: {total_average}')
```
这个代码示例创建了一个名为`Student`的类,用于存储每个学生的姓名、成绩、总成绩和平均分。然后,它创建了一个包含多个学生的列表,并使用`lambda`函数将学生按总成绩排序。最后,它计算所有学生的平均总成绩和平均分,并输出结果。
请注意,这只是一个简单的示例,你可能需要根据你的具体需求进行修改。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)