Python np.array的裁剪部分
我有一个numpy数组Python np.array的裁剪部分,python,arrays,numpy,Python,Arrays,Numpy,我有一个numpy数组 A.shape (512,270,1,20) 我不想使用维度4中的所有20层。新数组应该是 Anew.shape (512,270,1,2) 所以我想从中裁剪出数组A的两个“切片”,答案是: start = 4 # Index where you want to start. Anew = A[:,:,:,start:start+2] 可以使用而不是切片表示法来选择最终维度中的任意索引序列: x = np.zeros((512, 270, 1, 20)) y = x
A.shape
(512,270,1,20)
我不想使用维度4中的所有20层。新数组应该是
Anew.shape
(512,270,1,2)
所以我想从中裁剪出数组A的两个“切片”,答案是:
start = 4 # Index where you want to start.
Anew = A[:,:,:,start:start+2]
可以使用而不是切片表示法来选择最终维度中的任意索引序列:
x = np.zeros((512, 270, 1, 20))
y = x[..., [4, 10]] # the 5th and 11th indices in the final dimension
print(y.shape)
# (512,270,1,2)
谢谢,如果我不想在第一、第五、第十一场比赛中截取一个被截取的球。。。切片?读这篇优秀的文章,明白了,谢谢。只需使用[-1,-..,-..]即可到达切片,也可查看不规则切片。因此,您可以重新执行
A[:,:,:,[1,4,5]]
。