请尝试以下操作:
import numpy as np
data = np.array([
[[10, 10, 10], [0, 10, 10], [0, 10, 0]],
[[20, 0, 20], [20, 20, 0], [0, 20, 20]],
[[0, 30, 30], [30, 0, 30], [0, 30, 30]],
])
columns_to_replace = [0, 1]
value_to_replace = 1000
# Create a boolean mask for the zeros
mask = data[:, columns_to_replace, :] == 0
# Use the mask to replace the zeros with the desired value
data[:, columns_to_replace, :][mask] = value_to_replace
print(data)
我得到的结果是:
[[[ 10 10 10]
[1000 10 10]
[ 0 10 0]]
[[ 20 1000 20]
[ 20 20 1000]
[ 0 20 20]]
[[1000 30 30]
[ 30 1000 30]
[ 0 30 30]]]