[英]Slicing 3d numpy array - misunderstanding
I know for 3d numpy array I can index like: 我知道对于3d numpy数组,我可以像这样进行索引:
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. x[:]
将返回完整数组,而不进行任何实际切片。 By that logic, so will x[:][:]
. 按照这种逻辑,
x[:][:]
也将如此。
As such, x[:][:][1]
is equivalent to x[1]
. 这样,
x[:][:][1]
等效于 x[1]
。 This is why it's not the same as x[:,:,1]
. 这就是为什么它与
x[:,:,1]
。
I like @ffisegydd's answer, but I wanted to point out that this is not unique to numpy arrays. 我喜欢@ffisegydd的答案,但我想指出,这不是numpy数组所独有。 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: 在python中,语句
result = A[i, j]
等效于result = A[(i, j)]
,而语句result = A[i][j]
等效于:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.