题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
# 成绩排序 number = int(input()) rank = int(input()) data = [input().split(" ") for x in range(number)] # print(number, rank, data) def one_idea(mode, test_data): """ 思路: 创建2个字典{原始顺序1: 姓名}和{原始顺序2: 分数},原始顺序1==原始顺序2, 分数排序之后通过反推:分数->顺序->姓名 """ mode = True if mode < 1 else False name_dic = dict() grade_dic = dict() result = list() for i in test_data: name_dic[test_data.index(i)] = i[0] grade_dic[test_data.index(i)] = i[1] # 得到排序后的分数 grade_arr = list(map(int, grade_dic.values())) grade_arr.sort(reverse=mode) # 结果处理 for d in grade_arr: for k in grade_dic.keys(): if grade_dic[k] == str(d): result_str = [name_dic.get(k), str(d)] # 做重复判断 if result_str not in result: result.append(result_str) return result def other_idea(mode, test_data): """ 思路:重点使用list.sort(key=None, reverse=False)中的key参数,即可把列表的多个元组进行排序 """ mode = True if mode < 1 else False test_data.sort(key=lambda x: int(x[1]), reverse=mode) return test_data if __name__ == '__main__': # output = one_idea(rank, data) output = other_idea(rank, data) # 打印结果 for r in output: print(" ".join(r))