如何在 PYTHON 中按姓名和年龄排序?我在 .txt 文件中有以下列表:
John, 14
Mike, 18
Marco, 25
Michael, 33
我想按姓名和年龄排序。我写了这段代码,但它不起作用:
file = open("people.txt", "r")
data = file.readlines()
i = 0
for 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])
非常感谢你们 :)
相关分类