我需要使用索引列表將 3d NumPy 數(shù)組的多個值替換為其他值。例如:old_array = np.full((224,224,3),10)# in below list first position element are indexes and 2nd one are their corresponding valueslist_idx_val = [[array([[ 2, 14, 0], [ 2, 14, 1], [ 2, 14, 2], [99, 59, 1], [99, 61, 1], [99, 61, 2]], dtype=uint8), array([175, 168, 166,119, 117, 119], dtype=uint8)]#need to doold_array[2,14,1] =168一種方法是簡單地訪問索引值并用新值替換舊值。但 NumPy 數(shù)組和索引列表相當大。我將非常感謝一個快速有效的解決方案(沒有循環(huán),最好是切片或其他可能)來替換數(shù)組值,因為我需要通過使用這樣的索引列表以最小的延遲替換值來創(chuàng)建數(shù)千個數(shù)組。
1 回答
不負相思意
TA貢獻1777條經(jīng)驗 獲得超10個贊
讓我們進行數(shù)組索引:
idx, vals = list_idx_val old_array[idx[:,0], idx[:,1], idx[:,2]] = vals
輸出 (plt.show):

添加回答
舉報
0/150
提交
取消
