如何在python中按最多两个轴对列表进行排序?

Ash*_*ar7 1 python arrays sorting numpy list

我想按列表中每个元素的第0个和第1个索引的最大值对列表进行排序.

例如,如果我有这样的列表:

[[2,1,2],
 [0,3,9],
 [1,2,9],
 [1,1,2],
 [3,2,7]]
Run Code Online (Sandbox Code Playgroud)

排序后,它应如下所示:

[[0,3,9],
 [3,2,7],
 [2,1,2],
 [1,2,9],
 [1,1,2]]
Run Code Online (Sandbox Code Playgroud)

那么这是一种有效的方法呢?

我更喜欢使用numpy,但也欢迎其他解决方案.

jpp*_*jpp 5

取前两列的行最大值argsort,然后反转以获得降序.

您尚未指定任何打破平局,因此这与您所需的输出不匹配.换句话说,您的问题没有独特的解决方案.

res = A[A[:, :2].max(1).argsort()[::-1]]

array([[3, 2, 7],
       [0, 3, 9],
       [1, 2, 9],
       [2, 1, 2],
       [1, 1, 2]])
Run Code Online (Sandbox Code Playgroud)