我有一个深度嵌套的嵌套列表元组,如下所示:
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,因为它有50
的second lowest value,最后应该是third element,因为它有perform a 2 level sorting5
的third 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)
)
如有任何帮助,我们不胜感激!
提前谢谢!