输入10个学生5门课的成绩,用函数实现下列功能 计算平均分方差
时间: 2024-06-13 19:08:08 浏览: 109
以下是计算平均分方差的Python代码:
```python
def average(lst):
return sum(lst) / len(lst)
def variance(lst):
avg = average(lst)
return sum([(x - avg) ** 2 for x in lst]) / len(lst)
# 假设成绩存储在一个10行5列的二维列表中,名为scores
scores = [[80, 90, 70, 60, 85], [75, 85, 95, 70, 80], [90, 80, 70, 60, 75], [85, 75, 80, 90, 70], [70, 60, 75, 85, 95], [80, 90, 70, 60, 85], [75, 85, 95, 70, 80], [90, 80, 70, 60, 75], [85, 75, 80, 90, 70], [70, 60, 75, 85, 95]]
# 计算每个学生的平均分
student_avg = [average(row) for row in scores]
# 计算每门课程的平均分
course_avg = [average([row[i] for row in scores]) for i in range(5)]
# 找出所有50个分数中最高的分数所对应的学生和课程
max_score = max([max(row) for row in scores])
max_student, max_course = [(i+1, j+1) for i, row in enumerate(scores) for j, score in enumerate(row) if score == max_score][0]
# 计算平均分方差
variance_score = [variance([row[i] for row in scores]) for i in range(5)]
avg_variance = average(variance_score)
print("每个学生的平均分:", student_avg)
print("每门课程的平均分:", course_avg)
print("最高分数所对应的学生和课程:", "学生{},课程{}".format(max_student, max_course))
print("平均分方差:", avg_variance)
```
阅读全文