我有用户输入的数字,但是当我按降序输入时,只有列表被排序,否则它没有。
#007bff
答案 0 :(得分:0)
当你使用sorted(templist)时,你的列表元组被排序而不是内部列表!
您需要遍历内部列表并单独对其进行排序,例如
map(sorted,templist)
或
[sorted(l) for l in templist]
即,
>>> values = input("Enter the number")
Enter the number"5,4,3,2,1"
>>> values1 = input("Enter the number")
Enter the number"9,8,7,6"
>>> values2 = input("Enter the number")
Enter the number"8,6,4,2"
>>> templist = values.split(","),values1.split(","),values2.split(",")
>>> templist
(['5', '4', '3', '2', '1'], ['9', '8', '7', '6'], ['8', '6', '4', '2'])
>>> print('List : ',templist)
('List : ', (['5', '4', '3', '2', '1'], ['9', '8', '7', '6'], ['8', '6', '4', '2']))
>>> sorted(templist)
[['5', '4', '3', '2', '1'], ['8', '6', '4', '2'], ['9', '8', '7', '6']]
>>>
>>> map(sorted,templist)
[['1', '2', '3', '4', '5'], ['6', '7', '8', '9'], ['2', '4', '6', '8']]
>>>
>>> #or
...
>>> [sorted(l) for l in templist]
[['1', '2', '3', '4', '5'], ['6', '7', '8', '9'], ['2', '4', '6', '8']]
这是字符串的排序列表。
如果要对整数列表进行排序,
>>> [sorted(int(n) for n in l) for l in templist]
[[1, 2, 3, 4, 5], [6, 7, 8, 9], [2, 4, 6, 8]]