假设我们有以下的2D列表:
test_scores = [
[65, 75, 85],
[90, 85, 80],
[70, 60, 50],
]
要计算整个列表的平均分数,我们需要首先计算每个学生的平均分数,然后将这些数值相加并除以学生总数。
# 计算并存储每个学生的平均分数
student_averages = []
for student_scores in test_scores:
total = sum(student_scores)
average = total / len(student_scores)
student_averages.append(average)
# 计算整个组中所有学生的平均分数
class_total = sum(student_averages)
class_average = class_total / len(student_averages)
print("整个组的平均分数:", class_average)
要计算每个学生的百分比,我们需要首先计算每个学生的总分数和测试的总分数,然后将这两个数相除并将结果乘以100,以得到一个百分比数值。
# 计算和存储每个学生的总分数和百分比
student_percentages = []
for student_scores in test_scores:
total = sum(student_scores)
percentage = (total / 300) * 100 # 300是3个学生的总分数
student_percentages.append(percentage)
print("每个学生的百分比:", student_percentages)
输出:
整个组的平均分数: 73.33333333333333
每个学生的百分比: [75.0, 85.0, 60.0]