簡體   English   中英

將切片組合成用於 numpy 數組切片的多維切片

[英]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 所指出的,多維切片實際上是一個tupleslice對象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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM