I know for 3d numpy array I can index like:
item = x[0,2,1]
or
item = x[0][2][1]
But slicing work strange for me:
item = x[:,:,1]
is not the same as:
item = x[:][:][1]
What did I miss?
I know for 3d numpy array I can index like:
item = x[0,2,1]
or
item = x[0][2][1]
But slicing work strange for me:
item = x[:,:,1]
is not the same as:
item = x[:][:][1]
What did I miss?
x[:] will return the full array, without doing any actual slicing. By that logic, so will x[:][:].
As such, x[:][:][1] is equivalent to x[1]. This is why it's not the same as x[:,:,1].
id(x) and id(x[:]) they return different values though. So does that not make it a copy? I'm happy to change it, otherwise. EDIT: never mind I spoke to some wiser people than me, and they suspect it isn't a copy, so I'll edit it now.I like @ffisegydd's answer, but I wanted to point out that this is not unique to numpy arrays. In python the statement result = A[i, j] is equivalent to result = A[(i, j)] and the statement result = A[i][j] is equivalent to:
tmp = A[i]
result = tmp[j]
So if I use a dictionary:
A = {0 : "value for key 0",
(0, 1) : "value for key (0, 1)"}
print(A[0][1])
# a
print(A[0, 1])
# value for key (0, 1)