我需要使用索引列表將 3d NumPy 數組的多個值替換為其他值。例如: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 數組和索引列表相當大。我將非常感謝一個快速有效的解決方案(沒有循環,最好是切片或其他可能)來替換數組值,因為我需要通過使用這樣的索引列表以最小的延遲替換值來創建數千個數組。
1 回答

不負相思意
TA貢獻1777條經驗 獲得超10個贊
讓我們進行數組索引:
idx, vals = list_idx_val old_array[idx[:,0], idx[:,1], idx[:,2]] = vals
輸出 (plt.show
):
添加回答
舉報
0/150
提交
取消