np.ogrid
can be a convenient way to create expressions based on indexes in an array. For example,
import numpy as np
A = np.arange(27).reshape([3,3,3])
B = np.zeros(A.size)
i, j, k = np.ogrid[0:3, 0:3, 0:3]
B[i+j+k] = A
print(B)
gives
[ 0. 9. 18. 21. 24. 25. 26. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
Please note that the appointment
B[X] = A
equivalently
B[X.ravel()] = A.ravel()
. , X
, B
. , , .