第1关 输入数据创建列表
score_ls = []for i in range(10): a = float(input()) score_ls.append(a) print(score_ls)
第2关 计算每位选手的成绩
score_ls = []for i in range(10): a = float(input()) score_ls.append(a)print(score_ls)print(f'最高分{max(score_ls):.3f},最低分{min(score_ls):.3f},平均分{sum(score_ls) / len(score_ls):.3f}')
第3关 去除一个最高分和一个最低分
score_ls = []for i in range(10): a = float(input()) score_ls.append(a) print(score_ls)score_ls.remove(max(score_ls))score_ls.remove(min(score_ls))print(score_ls)print(f'最高分{max(score_ls):.3f},最低分{min(score_ls):.3f},平均分{sum(score_ls) / len(score_ls):.3f}')
第4关 计算中位数成绩
score_ls = []for i in range(10): a = float(input()) score_ls.append(a) print(score_ls)score_ls.remove(max(score_ls))score_ls.remove(min(score_ls))score_ls.sort() print(score_ls)print(f'去掉最高分和最低分后,中位数成绩为:{(score_ls[len(score_ls)//2]+score_ls[len(score_ls)//2-1])/2:.3f}')
第5关 去除弃权评委成绩
score_ls = []for i in range(10): a = input() score_ls.append(a) print(score_ls)while '' in score_ls: score_ls.remove('')print(score_ls)score_ls = list(map(float, score_ls))score_ls.remove(max(score_ls))score_ls.remove(min(score_ls))print(score_ls) print(f'{max(score_ls):.3f}, {min(score_ls):.3f}, {sum(score_ls) / len(score_ls):.3f}')
第6关 将平均成绩附加到列表末尾
score_ls = []for i in range(10): a = input() score_ls.append(a) print(score_ls)while '' in score_ls: score_ls.remove('')score = list(map(str, score_ls))score_ls = list(map(float, score_ls))score_ls.remove(max(score_ls))score_ls.remove(min(score_ls))avg_score = sum(map(float, score_ls))/len(score_ls) score.append(str(round(avg_score, 2)))print(score)
第7关 读文件到二维列表
def file_to_lst(file,n): list1 = [] with open(file) as fr: for i in range(n): score_ls = fr.readline().strip() score_ls = score_ls.split(',') list1.append(score_ls) print(list1) if __name__ == '__main__': filename = '/data/bigfiles/mark_score.csv' n = int(input()) file_to_lst(filename,n)
第8关 二维列表数据分析
def file_to_lst(file, n): """将文件名变量file指向的文件中的用逗号分隔的数据转为二维列表""" list1 = [] with open(file) as fr: # 创建文件对象 for i in range(n): score_ls = fr.readline().strip() score_ls = score_ls.split(',') list1.append(score_ls) return list1 if __name__ == '__main__': filename = '/data/bigfiles/mark_score.csv' n = int(input()) list2=[] for lst in file_to_lst(filename, n): # 遍历二维列表,lst为子列表 while '' in lst: # 循环判定子列表中是否还有空字符串 lst.remove('') # 移除子列表中的空字符串 lst = list(map(float, lst)) # 当前子列表转浮点数 lst.remove(max(lst)) lst.remove(min(lst)) avg_score = sum(map(float, lst))/len(lst) lst.append(float(round(avg_score, 2))) list2.append(lst) print(list2)
第9关 成绩排序
def file_to_lst(file): with open(file) as fr: score_ls = [line.strip().split(',') for line in fr] return score_ls def del_null_string(score_ls): score_ls_float = [] for lst in score_ls: while '' in lst: lst.remove('') lst = list(map(float,lst)) score_ls_float.append(lst) return score_ls_float def add_avg(score_ls_float): for i in range(len(score_ls_float)): score_ls_float[i].remove(max(score_ls_float[i])) score_ls_float[i].remove(min(score_ls_float[i])) avg = sum(score_ls_float[i]) / len(score_ls_float[i]) score_ls_float[i].append(round(avg,2)) return score_ls_float if __name__ == '__main__': filename = '/data/bigfiles/mark_score.csv' n = int(input()) score_lst = file_to_lst(filename) score_lst_f = del_null_string(score_lst) score_avg = add_avg(score_lst_f) print(sorted(score_avg, key=lambda x: (x[-1],-x[-2]),reverse=True)[:n])