用python 解决给出一个班的成绩列表如下
用python 解决给出一个班的成绩列表如下 [[许澄巍,34][刘德泽,72],[程海超,96],[邓海阳,88],[邓海荣,34],[陈海逸,98],[宋海昌,74],[徐瀚钰,44],[徐瀚文,92],[陈涵亮,57],[程涵煦,62],[宋明字,33],[徐涵衍,50],[万浩晶,46],[万浩波,66],[荣浩博,50],[陈浩初,61],[陈浩宕,52],[赵浩歌,97],[周浩广,80],[周浩邈,65],[周浩气,43],[童浩恩,52],[徐浩言,94],[徐鸿宝,96],[许鸿波,60],[许鸿博,33],[许鸿才,40],[徐鸿畅,50],[许鸿畴,97],[宋鸿达,35],[徐鸿德,94],[徐鸿飞,94],[徐鸿风,78],[徐鸿福,97],[许鸿光,83],[徐鸿晖,81],[童鸿朗,65],[周鸿文,66],[童鸿轩,58],[宋鸿煊,68],[和鸿赛,98],[凯鸿远,37],[宋鸿云,87],[徐鸿哲,48],[徐鸿祯,97],[徐鸿卓,83],[徐嘉,58],[徐光济,64],[徐澎湃,56],[徐彭泽,69],[宋鹏池,43],[宋鹏海,60],[宋浦和,50],[宋浦泽,96],[方瑞渊,63],[方越泽,46],[方博耘,49],[方德运,75],[方辰字,30],[方辰皓,36],[方辰钊,51],[方辰铭,52],[方辰银,30],[方辰阳,78],[方辰韦,75],[方辰良,73],[方辰沛,37],[方晨轩,83],[方晨涛,69],[方晨濡,73],[方晨潍,88],[方鸿振,35],[方吉星,62],[方铭晨,50],[方起运,35],[方运凡,96],[方运凯,84],[方运鹏,36],[方运浩,74],[方运诚,31],[方运良,50],[方运鸿,45],[方运锋,70],[方运盛,72],[方运升,79],[方运杰,62],[方运跳,79],[方运骏,49],[方运凯,57],[方运乾,68],[方维运,72],[方运晨,98].[方运莱最值华,000豪,94].[方星爵,32][方星腾,31],[方星睿,68]] 请编写程序输出以下信息 (1)全班平均成绩
(2)全班最高成绩,最低成绩
(3)找出中位成绩(并且指明是谁)
(4)统计优、良中、及格、不及格各多少人
方法一:
import numpy as np # 成绩列表 scores = [ ["许澄巍", 34], ["刘德泽", 72], ["程海超", 96], ["邓海阳", 88], ["邓海荣", 34], ["陈海逸", 98], ["宋海昌", 74], ["徐瀚钰", 44], ["徐瀚文", 92], ["陈涵亮", 57], ["程涵煦", 62], ["宋明字", 33], ["徐涵衍", 50], ["万浩晶", 46], ["万浩波", 66], ["荣浩博", 50], ["陈浩初", 61], ["陈浩宕", 52], ["赵浩歌", 97], ["周浩广", 80], ["周浩邈", 65], ["周浩气", 43], ["童浩恩", 52], ["徐浩言", 94], ["徐鸿宝", 96], ["许鸿波", 60], ["许鸿博", 33], ["许鸿才", 40], ["徐鸿畅", 50], ["许鸿畴", 97], ["宋鸿达", 35], ["徐鸿德", 94], ["徐鸿飞", 94], ["徐鸿风", 78], ["徐鸿福", 97], ["许鸿光", 83], ["徐鸿晖", 81], ["童鸿朗", 65], ["周鸿文", 66], ["童鸿轩", 58], ["宋鸿煊", 68], ["和鸿赛", 98], ["凯鸿远", 37], ["宋鸿云", 87], ["徐鸿哲", 48], ["徐鸿祯", 97], ["徐鸿卓", 83], ["徐嘉", 58], ["徐光济", 64], ["徐澎湃", 56], ["徐彭泽", 69], ["宋鹏池", 43], ["宋鹏海", 60], ["宋浦和", 50], ["宋浦泽", 96], ["方瑞渊", 63], ["方越泽", 46], ["方博耘", 49], ["方德运", 75], ["方辰字", 30], ["方辰皓", 36], ["方辰钊", 51], ["方辰铭", 52], ["方辰银", 30], ["方辰阳", 78], ["方辰韦", 75], ["方辰良", 73], ["方辰沛", 37], ["方晨轩", 83], ["方晨涛", 69], ["方晨濡", 73], ["方晨潍", 88], ["方鸿振", 35], ["方吉星", 62], ["方铭晨", 50], ["方起运", 35], ["方运凡", 96], ["方运凯", 84], ["方运鹏", 36], ["方运浩", 74], ["方运诚", 31], ["方运良", 50], ["方运鸿", 45], ["方运锋", 70], ["方运盛", 72], ["方运升", 79], ["方运杰", 62], ["方运跳", 79], ["方运骏", 49], ["方运凯", 57], ["方运乾", 68], ["方维运", 72], ["方运晨", 98], ["方运莱最值华,000豪,94", 94], ["方星爵", 32], ["方星腾", 31], ["方星睿", 68] ] # 提取成绩部分 scores_only = [score[1] for score in scores] # (1) 计算全班平均成绩 average_score = np.mean(scores_only) # (2) 找出全班最高成绩和最低成绩 max_score = np.max(scores_only) min_score = np.min(scores_only) # (3) 找出中位成绩及对应学生 median_score = np.median(scores_only) median_student = [score[0] for score in scores if score[1] == median_score] # (4) 统计优、良、中、及格、不及格各多少人 excellent_count = np.sum(np.array(scores_only >= 90)) good_count = np.sum(np.logical_and(scores_only >= 80, scores_only < 90)) pass_count = np.sum(np.logical_and(scores_only >= 60, scores_only < 80)) fail_count = np.sum(scores_only < 60) print("平均成绩:",average_score) print("最大成绩:",max_score) print("最小成绩:",min_score) print("中位成绩及对应学生:",median_score,median_student)
方法二:
# 成绩列表 scores = [ ["许澄巍", 34], ["刘德泽", 72], ["程海超", 96], ["邓海阳", 88], ["邓海荣", 34], ["陈海逸", 98], ["宋海昌", 74], ["徐瀚钰", 44], ["徐瀚文", 92], ["陈涵亮", 57], ["程涵煦", 62], ["宋明字", 33], ["徐涵衍", 50], ["万浩晶", 46], ["万浩波", 66], ["荣浩博", 50], ["陈浩初", 61], ["陈浩宕", 52], ["赵浩歌", 97], ["周浩广", 80], ["周浩邈", 65], ["周浩气", 43], ["童浩恩", 52], ["徐浩言", 94], ["徐鸿宝", 96], ["许鸿波", 60], ["许鸿博", 33], ["许鸿才", 40], ["徐鸿畅", 50], ["许鸿畴", 97], ["宋鸿达", 35], ["徐鸿德", 94], ["徐鸿飞", 94], ["徐鸿风", 78], ["徐鸿福", 97], ["许鸿光", 83], ["徐鸿晖", 81], ["童鸿朗", 65], ["周鸿文", 66], ["童鸿轩", 58], ["宋鸿煊", 68], ["和鸿赛", 98], ["凯鸿远", 37], ["宋鸿云", 87], ["徐鸿哲", 48], ["徐鸿祯", 97], ["徐鸿卓", 83], ["徐嘉", 58], ["徐光济", 64], ["徐澎湃", 56], ["徐彭泽", 69], ["宋鹏池", 43], ["宋鹏海", 60], ["宋浦和", 50], ["宋浦泽", 96], ["方瑞渊", 63], ["方越泽", 46], ["方博耘", 49], ["方德运", 75], ["方辰字", 30], ["方辰皓", 36], ["方辰钊", 51], ["方辰铭", 52], ["方辰银", 30], ["方辰阳", 78], ["方辰韦", 75], ["方辰良", 73], ["方辰沛", 37], ["方晨轩", 83], ["方晨涛", 69], ["方晨濡", 73], ["方晨潍", 88], ["方鸿振", 35], ["方吉星", 62], ["方铭晨", 50], ["方起运", 35], ["方运凡", 96], ["方运凯", 84], ["方运鹏", 36], ["方运浩", 74], ["方运诚", 31], ["方运良", 50], ["方运鸿", 45], ["方运锋", 70], ["方运盛", 72], ["方运升", 79], ["方运杰", 62], ["方运跳", 79], ["方运骏", 49], ["方运凯", 57], ["方运乾", 68], ["方维运", 72], ["方运晨", 98], ["方运莱最值华,000豪,94", 94], ["方星爵", 32], ["方星腾", 31], ["方星睿", 68] ] # (1) 计算全班平均成绩 total_score = 0 for student in scores: total_score += student[1] average_score = total_score / len(scores) average_score = round(average_score, 2) # 保留两位小数 # (2) 找出全班最高成绩和最低成绩 max_score = scores[0][1] min_score = scores[0][1] max_student = [scores[0][0]] min_student = [scores[0][0]] for student in scores: if student[1] > max_score: max_score = student[1] max_student = [student[0]] if student[1] < min_score: min_score = student[1] min_student = [student[0]] # (3) 找出中位成绩及对应学生 sorted_scores = sorted(scores, key=lambda x: x[1]) middle_index = len(sorted_scores) // 2 median_score = sorted_scores[middle_index][1] median_student = [student[0] for student in scores if student[1] == median_score] # (4) 统计优、良、中、及格、不及格各多少人 excellent_count = 0 good_count = 0 pass_count = 0 fail_count = 0 for student in scores: score = student[1] if score >= 90: excellent_count += 1 elif 80 <= score < 90: good_count += 1 elif 60 <= score < 80: pass_count += 1 else: fail_count += 1 print("平均成绩:", average_score) print("最大成绩:", max_score, "最高分学生:", max_student) print("最小成绩:", min_score, "最低分学生:", min_student) print("中位成绩及对应学生:", median_score, median_student)
- 上一篇:用python求圆周率代码
- 下一篇:电子元件封装术语大全(上)