需要用数字对嵌套元组进行排序

时间:2021-02-10 09:06:23

标签: python numpy opencv

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

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 进行排序

1 个答案:

答案 0 :(得分: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]]
相关问题