我有一个深度嵌套的嵌套列表元组,如下所示:

ip = (array([[[ 50,  73]],
       [[ 50, 107]],
       [[ 55, 108]],
       [[ 55, 121]],
       [[978,  87]],
       [[977,  86]],
       [[977,  73]]], dtype=int32), 

array([[[ 669,    3]],
       [[ 668,    4]],
       [[ 667,    4]],
       [[1033,   71]],
       [[1035,   69]],
       [[1035,    4]],
       [[ 848,    4]],
       [[ 847,    3]],
       [[ 813,    3]],
       [[ 718,    4]],
       [[ 717,    3]]], dtype=int32), 

array([[[ 17,   3]],
       [[ 16,   4]],
       [[  0,   4]],
       [[  0,  49]],
       [[197,  49]],
       [[197,   8]],
       [[ 84,   4]],
       [[ 83,   3]]], dtype=int32))

上面例子中的主要元组的长度是3.我想在上面的 struct 上加到perform a 2 level sorting.首先,我想要sort all the 3 elements in the main list in increasing order based on the first value of nested list.因此,在上面的例子中,third element将首先出现,因为它有第一个元素的lowest value,即0.第二个应该是first element,因为它有50second lowest value,最后应该是third element,因为它有perform a 2 level sorting5third lowest value.第一级排序的输出应为:

op = (array([[[ 17,   3]],
       [[ 16,   4]],
       [[  0,   4]],
       [[  0,  49]],
       [[197,  49]],
       [[197,   8]],
       [[ 84,   4]],
       [[ 83,   3]]], dtype=int32),

array([[[ 50,  73]],
       [[ 50, 107]],
       [[ 55, 108]],
       [[ 55, 121]],
       [[978,  87]],
       [[977,  86]],
       [[977,  73]]], dtype=int32), 

array([[[ 669,    3]],
       [[ 668,    4]],
       [[ 667,    4]],
       [[1033,   71]],
       [[1035,   69]],
       [[1035,    4]],
       [[ 848,    4]],
       [[ 847,    3]],
       [[ 813,    3]],
       [[ 718,    4]],
       [[ 717,    3]]], dtype=int32), 
)

现在,我想对上面的op再次执行相同的排序,但我不想对嵌套列表的第一个值进行排序,而是希望对嵌套列表的sort based on the second value进行排序.因此,现在的最终输出如下所示:

final_op = (array([[[ 17,   3]],
       [[ 16,   4]],
       [[  0,   4]],
       [[  0,  49]],
       [[197,  49]],
       [[197,   8]],
       [[ 84,   4]],
       [[ 83,   3]]], dtype=int32), 

array([[[ 669,    3]],
       [[ 668,    4]],
       [[ 667,    4]],
       [[1033,   71]],
       [[1035,   69]],
       [[1035,    4]],
       [[ 848,    4]],
       [[ 847,    3]],
       [[ 813,    3]],
       [[ 718,    4]],
       [[ 717,    3]]], dtype=int32),

array([[[ 50,  73]],
       [[ 50, 107]],
       [[ 55, 108]],
       [[ 55, 121]],
       [[978,  87]],
       [[977,  86]],
       [[977,  73]]], dtype=int32)
)

如有任何帮助,我们不胜感激!

提前谢谢!

推荐答案

您可以在tuple上使用sorted,并使用key参数指定项目

第一次排序

ip = sorted(ip, key=lambda x: x[0][0][0])
print(ip)

[array([[[ 17,   3]],
       [[ 16,   4]],
       [[  0,   4]],
       [[  0,  49]],
       [[197,  49]],
       [[197,   8]],
       [[ 84,   4]],
       [[ 83,   3]]]),
array([[[ 50,  73]],
       [[ 50, 107]],
       [[ 55, 108]],
       [[ 55, 121]],
       [[978,  87]],
       [[977,  86]],
       [[977,  73]]]),
array([[[ 669,    3]],
       [[ 668,    4]],
       [[ 667,    4]],
       [[1033,   71]],
       [[1035,   69]],
       [[1035,    4]],
       [[ 848,    4]],
       [[ 847,    3]],
       [[ 813,    3]],
       [[ 718,    4]],
       [[ 717,    3]]])]

第二种是

ip = sorted(ip, key=lambda x: x[0][0][1])
print(ip)

[array([[[ 17,   3]],
       [[ 16,   4]],
       [[  0,   4]],
       [[  0,  49]],
       [[197,  49]],
       [[197,   8]],
       [[ 84,   4]],
       [[ 83,   3]]]),
array([[[ 669,    3]],
       [[ 668,    4]],
       [[ 667,    4]],
       [[1033,   71]],
       [[1035,   69]],
       [[1035,    4]],
       [[ 848,    4]],
       [[ 847,    3]],
       [[ 813,    3]],
       [[ 718,    4]],
       [[ 717,    3]]])
array([[[ 50,  73]],
       [[ 50, 107]],
       [[ 55, 108]],
       [[ 55, 121]],
       [[978,  87]],
       [[977,  86]],
       [[977,  73]]])]

如果你想要回tuple,只要做tuple(ip)就行了

Python相关问答推荐

如何使用scipy从频谱图中回归多个高斯峰?

将jit与numpy linSpace函数一起使用时出错

scikit-learn导入无法导入名称METRIC_MAPPING64'

如何制作10,000年及以后的日期时间对象?

如何过滤包含2个指定子字符串的收件箱列名?

Python中绕y轴曲线的旋转

OR—Tools CP SAT条件约束

如何在给定的条件下使numpy数组的计算速度最快?

将tdqm与cx.Oracle查询集成

Pre—Commit MyPy无法禁用非错误消息

在单个对象中解析多个Python数据帧

Python中的变量每次增加超过1

如何从列表框中 Select 而不出错?

Python—转换日期:价目表到新行

python sklearn ValueError:使用序列设置数组元素

使用Openpyxl从Excel中的折线图更改图表样式

不允许 Select 北极滚动?

Tensorflow tokenizer问题.num_words到底做了什么?

如何提高Pandas DataFrame中随机列 Select 和分配的效率?

Django.core.exceptions.SynchronousOnlyOperation您不能从异步上下文中调用它-请使用线程或SYNC_TO_ASYNC