[英]Sorting Numpy Array of two different types
我主要使用C ++編寫代碼,並且試圖弄清楚如何對自己設法創建的數據結構進行排序。 請找到以下代碼:
dtype = [('dist',float) , ('type',int)]
arr = np.zeros((200,2), dtype = dtype)
i = 0
for current_image in all_images:
arr[i][0] = distance(current_image, new_image)
if current_image[576] == 1:
arr[i][1] = 1
else:
arr[i][1] = 0
i = i + 1
我想在第一列中創建一個200 x 2的浮點數數組,在第二列中創建一個int數。 我不確定如何根據dist值(從最小到最大)對所有200個元素進行排序。 而且我的數據結構看起來很奇怪,像這樣:
[[( 9.47168802, 9) ( 0. , 0)]
[( 6.95162905, 6) ( 1. , 1)]
[( 8.72382552, 8) ( 0. , 0)]
[( 8.9333134 , 8) ( 1. , 1)]]
您可以簡單地對其進行排序:
arr.sort(axis=0)
您還可以明確提及sort鍵,盡管在您的情況下這是可選的,因為無論如何它是第一個字段:
arr.sort(axis=0, order='dist')
或制作副本而不是就地排序:
np.sort(arr, axis=1, order='dist')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.