在Python中对嵌套列表的嵌套元组进行两级排序

huangapple go评论80阅读模式
英文:

Two level sorting in a nested tuple of nested lists in Python

问题

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

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。我想对上述结构执行两级排序。首先,我想根据嵌套列表的第一个值,对主列表中的所有3个元素进行升序排序。所以在上面的情况下,第三个元素将首先出现,因为它具有第一个元素的最低值,即0。第二个应该是第一个元素,因为它具有第二低的值50,最后一个应该是第三个元素,因为它具有第三低的值1035。第一级排序的输出应为:

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执行相同的排序,但是不是根据嵌套列表的第一个值,而是根据嵌套列表的第二个值进行排序。所以现在最终的输出将如下所示:

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))

希望这有所帮助!

英文:

I have a deeply nested tuple of nested lists as follows:

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))

The length of the main tuple in above example is 3. I want to perform a 2 level sorting on the above structure. First I want to sort all the 3 elements in the main list in increasing order based on the first value of nested list. So in the above case the third element will come first as it has the lowest value of the first element i.e. 0. Second should be the first element as it has the second lowest value of 50and last should be the third element as it has the third lowest value of 1035. The output of the first level sorting should be:

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), 
)

Now I want to perform the same sorting again on the above op but instead of the first value of the nested list I want to sort based on the second value of the nested list. So now the final output would be as follows:

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)
)

Any help is appreciated!

Thanks in advance!

答案1

得分: 2

你可以在你的 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)

英文:

You can use sorted on your tuple and specify the item using the key parameter

First sort

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]]])]

And the second sort

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]]])]

If you want it back as tuple just do tuple(ip)

huangapple
  • 本文由 发表于 2023年7月17日 15:13:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定