3 回答
TA贡献1851条经验 获得超4个赞
我在这篇文章中得到了答案:沿动态指定的轴切片一个 numpy 数组 就我而言:
def slicer(array, indices, axis): selection = [slice(None)] * array.ndim selection[axis] = indices return tuple(selection)
slice(None)
相当于:
。最后:
array[slicer(array, indices, axis)] += 1
TA贡献1795条经验 获得超7个赞
import numpy as np
array = np.array([[[1, 2],
[3, 4],
[5, 6]],
[[7, 8],
[9, 10],
[11, 12]]])
indices = [0, 2]
# either like this
array[:, indices, :] += 1
# or using a for loop
for idx in range(len(array)):
array[idx, indices, :] += 1
print(array)
TA贡献1773条经验 获得超3个赞
如果你想要一个循环使用:
import numpy as np
array = np.array([[[1, 2],
[3, 4],
[5, 6]],
[[7, 8],
[9, 10],
[11, 12]]])
indices = [0, 2]
for i in array:
i[[0, 2]] += 1
添加回答
举报