![](/img/trans.png)
[英]Use Numpy Multidimensional Array Slicing Without Using the [slice,slice] Syntax?
[英]Composing slices into a multidimensional slice for numpy array slicing
如果我有兩個切片對象分別沿一維定義,是否可以將它們組合起來以獲得可用於切片 numpy 數組的多維切片對象?
mat = np.zeros((10,10), dtype=np.uint8)
s1 = slice(0,5)
s2 = slice(0,5)
mat[s1,s2] # I want to achieve this effect with one slice object
slice2d = slice(s1, s2) # does not throw an error
mat[slice2d] # but this does not work
正如@unutbu 所指出的,多維切片實際上是一個tuple
或slice
對象list
,然后:
slice2d = (s1, s2)
mat[slice2d]
將工作。 同樣,您可以將其擴展到 3-D, ..., ND 數組:
slice3d = (s1, s2, s3)
...
sliceNd = (s1, s3, s3, ..., sN)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.