簡體   English   中英

如何按 python 中特定值的特定索引對列表元素進行排序

[英]How to sort list elements by specific indexes of specific values in python

我有 500000 個 3D 坐標和 500000 個 rgb 補丁的數據集。 首先,我想在從數據集中刪除一些錯誤數據后,對 3D 坐標進行排序,使其在開始時具有最相似的值。 為此,我進行了如下操作:

# Loading saved Dataset
print("Begin Loding Dataset ...")
X = np.load('train_RGB-Patches_Fire_seq01.npy')
Y = np.load('train_3DPoses-Patches_Fire_seq01.npy')
print("End Loading Dataset => Shapes : ",X.shape, Y.shape)
print("max - min 3D originals : ",Y.max()," , ", Y.min())
print("Begin Correcting 3D_Patches Dataset ...")
Y_faux_indx = np.unique(np.argwhere(Y>15)[:,0].reshape((-1,1)))
Y_correct = np.delete(Y,Y_faux_indx,0)
X_correct = np.delete(X,Y_faux_indx,0)
Y_sorted = np.array(sorted(Y_correct.tolist())).reshape((-1,4))
print("End Correcting 3D_Patches Dataset ...")

現在根據排序后的 3D 標簽,我想從之前未排序的校正數據中獲取這些排序數據的索引,然后根據這些索引排列 rgb 數據。 為此,我制作了這段代碼,它需要很長時間才能執行:

print("Begin Sorting 3D_Patches Dataset ...")
sorted_dataset_indx = []
for j in range(len(Y_sorted)):
    element_verification = Y_correct == Y_sorted[j]
    for i in range(len(element_verification)):
        if element_verification[i].prod()==1:
            if i not in sorted_dataset_indx:
                sorted_dataset_indx.append(i)
 sorted_dataset_indx = np.array(sorted_dataset_indx)
 X_sorted = X_correct[sorted_dataset_indx]
 print("End Sorting 3D_Patches Dataset => Shapes : ",X_sorted.shape,Y_sorted.shape)
 print("max - min 3D new : ",Y_sorted.max()," , ", Y_sorted.min())

所以我想要另一個解決方案來幫助我更快地執行這個?

使用循環——尤其是嵌套循環——通常會很慢,應該盡可能避免。
我顯然無法重現您的代碼,但這是一個適合您的用例的玩具示例:

list1 = [1, 10, 2, 3, 5, 0, 1.5]
list2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
enumerated_list1 = list(enumerate(list1))
enumerated_list1.sort(key=lambda x: x[1])
sorting_inds = [x[0] for x in enumerated_list1]
list2_sorted_by_list1 = [list2[i] for i in sorting_inds]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM