700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python二维数组排序_Python排序多维数组

python二维数组排序_Python排序多维数组

时间:2023-06-26 06:59:34

相关推荐

python二维数组排序_Python排序多维数组

我需要按照特定的元素对数组进行排序。

This is an array:arr=

[0, [71, 554, 258, 793]]

[1, [61, 415, 148, 593]]

[2, [91, 145, 658, 893]]

I need to be able to sort it by arr[0][0] as well as by any element from the internal array like arr[0][1] or arr[0][2]

currently, I'm able to sort it by using key=itemgetter(1) where: itemgetter(1) - is second element of array [0, [71, 554, 258, 793]] in this cese = 71from operator import itemgetter

array = sorted(array, key=itemgetter(1))

print(*array[:], sep="\n")

how to sort this array by any element from the internal array [71, 554, 258, 793]?

so if I'm sorting by the second element from internal array output should be like this: (column 145, 415, 554)arr=

[2, [91, 145, 658, 893]]

[1, [61, 415, 148, 593]]

[0, [71, 554, 258, 793]]

如果我从内部数组输出按第三个元素进行排序,应该如下所示:(第148、258、658列)arr=

[1, [61, 415, 148, 593]]

[0, [71, 554, 258, 793]]

[2, [91, 145, 658, 893]]

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。