Python 列表操作:统计学生成绩分析
score = [40, 55, 78, 89, 90, 89, 99, 67, 90, 92]
(1) 求所有学生成绩的最高值。
max_score = max(score) print('最高分数是:', max_score)
(2) 求所有学生成绩的最低值。
min_score = min(score) print('最低分数是:', min_score)
(3) 求所有学生成绩的平均分。
avg_score = sum(score) / len(score) print('平均分数是:', avg_score)
(4) 统计不及格的学生人数。
fail_num = sum([1 for s in score if s < 60]) print('不及格的学生人数是:', fail_num)
(5) 统计90分以上的学生人数。
above_90_num = sum([1 for s in score if s >= 90]) print('90分以上的学生人数是:', above_90_num)
(6) 找出60-80之间的所有成绩。
between_60_80 = [s for s in score if 60 <= s <= 80] print('60-80之间的成绩是:', between_60_80)
(7) 找出所有相同分数的成绩。
same_score = set([s for s in score if score.count(s) > 1]) print('相同分数的成绩是:', same_score)
(8) 将成绩从高到低排序。
sorted_score = sorted(score, reverse=True) print('按成绩从高到低排序:', sorted_score)
(9) 查找第二名的考试分数。
second_score = sorted_score[1] print('第二名的考试分数是:', second_score)
(10) 查找某一成绩(78分)在班内排第几名。
rank = sorted_score.index(78) + 1 print('78分的学生在班内排名:第', rank, '名')
原文地址: https://www.cveoy.top/t/topic/n7Na 著作权归作者所有。请勿转载和采集!