简体   繁体   English

如何通过多个键对3D数组排序

[英]How to sort a 3d array by multiple keys

I need to sort a 3d array by multiple keys, and i'm not familiar with lambda. 我需要通过多个键对3d数组进行排序,而我对lambda并不熟悉。 I have an array: 我有一个数组:

arry = [,
    [[3,1,2], 'foo', 'bar'],
    [[1,2,3], 'foo', 'bar'],
    [[2,1,3], 'foo', 'bar']

    #[[x,y,z], 'blah', 'blah']
]

And i need to sort it so that the z value is highest , but if the z values equal, to sort by the y value lowest , and if y is equal, sort by x lowest . 我需要对其进行排序,以使z值最高 ,但是如果z值相等,则按y最低进行排序,如果y相等,则按x 最低进行排序。 So the output array should look like this: 因此,输出数组应如下所示:

arry = [
    [[2,1,3], 'foo', 'bar'],
    [[1,2,3], 'foo', 'bar'],
    [[3,1,2], 'foo', 'bar']

    #[[x,y,z], 'blah', 'blah']
]

Use sorted with key : 使用key sorted

sorted(arr, key=lambda x:(-x[0][2], x[0][1], x[0][0]))

Output: 输出:

[[[2, 1, 3], 'foo', 'bar'],
 [[1, 2, 3], 'foo', 'bar'],
 [[3, 1, 2], 'foo', 'bar']]

Note the - sign on x[0][2] to implement different criteria of comparison. 注意x[0][2]上的-号可实现不同的比较标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM