I have a 3d list formed by
myArray = np.array([[[0]*n for i in range(m)] for j in range(o)])
I have a loop that runs over all elements, and increments the value stored in the current element and a number of elements in the neighborhood of the current element:
myArray[xa:xb][ya:yb][za:zb] += 1.
where xa,xb, etc. are generated according to the current element considered in the loop, and not necessarily the same. In other words, I'd like to increment the values of a given sub-triangle in the 3D list.
However, when I try to address myArray[xa:xb][0][0], I get a list with length that is larger than len(myArray[0]). Not to mention myArray[xa:xb][ya:yb][za:zb] += 1 results in more elements to be incremented by 1 than desired.
I could achieve this by using three nested for loops:
for i in range(xa,xb+1):
for j in range(ya,yb+1):
for k in range(za,zb+1):
myArray[i][j][k] += 1
but this slows down the code a lot. What can I do to achieve this without such a loss of performance?
myArray = np.zeros((o, m, n))and check the dimensions of an array or a slice withmyArray.shapeormyarray[xa:xb][0].shape, etc. It might be that the dimensions ofmyArrayare not in the order you expect. Also I would not recommend usingoas a variable name...myArray[xa:xb][0]is still a 2D array, equivalent tomyArray[xa].