Enter your name, age & score: a 17 99
Enter your name, age & score: a 19 98
Enter your name, age & score: a 19 100
Enter your name, age & score: a 19 99
Enter your name, age & score: b 19 99
Enter your name, age & score: b 19 98
Enter your name, age & score: b 19 100
Enter your name, age & score: -1
输出:
(('a', '17', '99'), ('a', '19', '100'), ('a', '19', '98'), ('a', '19', '99'), ('b', '19', '100'), ('b', '19', '98'), ('b', '19', '99'))
输出应该是这样的:
(('a','17','99'),('a','19','98'),('a','19','99'),('a' ,'19','100'),('b','19','98'),('b','19','99'),('b','19','100' ))
我该怎么做?这是我的代码..
def check(txt):
global c
if txt.count(" ") == 2:
tup=(tuple(txt.split(" ")))
list1.append(tup)
list1.sort()
else:
if txt != "-1":
c= 1
return c
main() # prints the converted list to tuple..
答案 0 :(得分:1)
元组正确排序,因为它们只包含字符串。如果要以数字方式排序,请将值存储为整数:
parts = txt.split(" ")
list1.append(tuple(parts[0], int(parts[1]), int(parts[2]))
答案 1 :(得分:1)
尝试sorted
sorted(your_tup, key=lambda x: [x[0], int(x[1]), int(x[2])])