-
繁花不似锦
我在这篇文章中得到了答案:沿动态指定的轴切片一个 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
-
一只萌萌小番薯
import numpy as nparray = np.array([[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10], [11, 12]]])indices = [0, 2]# either like thisarray[:, indices, :] += 1# or using a for loopfor idx in range(len(array)): array[idx, indices, :] += 1print(array)
-
慕容3067478
如果你想要一个循环使用:import numpy as nparray = 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