Python 需要使用数字对嵌套元组进行排序
我试图对元组进行如下排序Python 需要使用数字对嵌套元组进行排序,python,numpy,opencv,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
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对其进行排序,您的意思是:
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]]
sorted_led_ROI=ROI
不会创建数据的副本-它只是以另一个名称提供相同的数据。您正在覆盖sorted\u led\u ROI[index]=ROI[y]
中的数据,从而导致此混合结果。您希望有一个单独的复制数组根据原始数组进行排序。感谢您的回复。。我同意我做了很多重复工作,把一切都搞混了。。打印(ROI[ROI[:,0].argsort()])这有助于解决