如何在 PYTHON 中按姓名和年龄排序?我在 .txt 文件中有以下列表:John, 14Mike, 18Marco, 25Michael, 33我想按姓名和年龄排序。我写了这段代码,但它不起作用:file = open("people.txt", "r")data = file.readlines()i = 0for line in data: name, age = line.split(',') list = [name, age] i += 1 print("For sorting by name press (1);") print("For sorting by age press (2);") z = eval(input()) if z == 1: list.sort(key=lambda x: x.name, reverse=True) print([item.name for item in list])非常感谢你们 :)
添加回答
举报
0/150
提交
取消