【问题标题】:Need to sort a nested tuple with numbers [duplicate]需要用数字对嵌套元组进行排序[重复]
【发布时间】:2021-05-13 23:00:44
【问题描述】:

我试图对元组进行如下排序

input: ROI: 
[[191  60  23  18]
 [143  60  23  19]
 [ 95  52  24  21]
 [237  51  24  21]
 [ 47  38  27  22]
 [281  35  25  22]
 [  4  17  26  24]
 [324  13  22  21]]

Expected Output = S_ROI: 
[[4  17  26  24]
 [47  38  27  22]
 [ 95  52  24  21]
 [143  60  23  19]
 [ 191  60  23  18]
 [237  51  24  21]
 [281  35  25  22]
 [324  13  22  21]]

我有中间数组

column=[191 143 95 237 47 281 4 324]

我已经尝试过了 - 但投资回报率正在循环内更新

sort_index = np.argsort(column) 
column.sort()

sorted_led_ROI=ROI;
index=0
    for y in sort_index:
        sorted_led_ROI[index]=ROI[y]
        index =index+1
    print('sorted_led_ROI:', sorted_led_ROI)

结果:

sorted_led_ROI: 
[[  4  17  26  24]
 [ 47  38  27  22]
 [ 95  52  24  21]
 [ 47  38  27  22]
 [  4  17  26  24]
 [ 47  38  27  22]
 [ 47  38  27  22]
 [324  13  22  21]]

帮助我在 python 中使用 np 或 cv 对其进行排序

【问题讨论】:

  • sorted_led_ROI=ROI 不会创建数据的副本 - 它只是使相同的数据以另一个名称可用。您正在覆盖sorted_led_ROI[index]=ROI[y] 中的数据,导致这种混淆结果。您希望根据原始数组对单独的复制数组进行排序。
  • 感谢您的回复.. 我同意我做了很多重复并混淆了所有内容.. print(ROI[ROI[:,0].argsort()]) 这有助于解决跨度>

标签: python numpy opencv


【解决方案1】:

你的意思是这样吗:

print(ROI[ROI[:,0].argsort()])

输出:

[[  4  17  26  24]
 [ 47  38  27  22]
 [ 95  52  24  21]
 [143  60  23  19]
 [191  60  23  18]
 [237  51  24  21]
 [281  35  25  22]
 [324  13  22  21]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 2015-06-28
    相关资源
    最近更新 更多