Python 用索引列表替换3d Numpy数组的多个值
我需要使用索引列表将3d NumPy数组的多个值替换为其他值。例如:Python 用索引列表替换3d Numpy数组的多个值,python,arrays,numpy,indexing,Python,Arrays,Numpy,Indexing,我需要使用索引列表将3d NumPy数组的多个值替换为其他值。例如: old_array = np.full((224,224,3),10) # in below list first position element are indexes and 2nd one are their corresponding values list_idx_val = [[array([[ 2, 14, 0], [ 2, 14, 1], [ 2, 14, 2], [99, 59,
old_array = np.full((224,224,3),10)
# in below list first position element are indexes and 2nd one are their corresponding values
list_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 do
old_array[2,14,1] =168
一种方法是简单地访问索引值并用新值替换旧值。但是NumPy数组和索引列表相当大。我非常感谢有一个快速高效的解决方案(没有循环,最好是切片或其他方式)来替换数组值,因为我需要通过使用此类索引列表以最小的延迟替换值来创建数千个数组。让我们来做数组索引:
idx, vals = list_idx_val
old_array[idx[:,0], idx[:,1], idx[:,2]] = vals
输出(plt.show
):
旧数组[2,14,1]不应该变成168吗?谢谢@LevB。。我已经更新了问题。