Python列表排序给出了错误的结果

时间:2017-06-24 14:50:53

标签: python sorting

我想在Python中对列表进行排序。我试图做的是:

activities = [[3,1,0,5,8,5],[4,2,6,7,9,9]]
activities.sort(key=lambda x: x[1])
print(activities)

但它不起作用。

我希望列表的排序方式如下:

[1,3,0,5,8,5]
[2,4,6,7,9,9]

2 个答案:

答案 0 :(得分:0)

所以你想交换每个子列表的前两个元素,然后排序?

activities = [[3,1,0,5,8,5],[4,2,6,7,9,9]]
activities = [a[1::-1] + a[2:] for a in activities]
activities.sort(key=lambda x: x[0])
print(activities)

答案 1 :(得分:-1)

您需要分别对2个子列表进行排序:

activities = [[3,1,0,5,8,5],[4,2,6,7,9,9]]
activities = activities[0].sort() 
activities = activities[1].sort() 
print(activities)