问题描述
在Numpy(和Python一般来说,我想)中,如何存储切片索引,例如(...,0,:),以便传递它并将其应用于各种数组?例如,能够将切片索引传递给函数和从函数传递,这将是很好的。
In Numpy (and Python in general, I suppose), how does one store a slice-index, such as (...,0,:), in order to pass it around and apply it to various arrays? It would be nice to, say, be able to pass a slice-index to and from functions.
推荐答案
Python使用切片语法创建特殊对象,但仅在方括号内创建索引。您可以手动创建这些对象(在这种情况下,(...,0,:))
是(省略号,0,切片(无) ,无,无))
,或者你可以创建一个小帮助对象:
Python creates special objects out of the slice syntax, but only inside the square brackets for indexing. You can either create those objects by hand (in this case, (...,0,:)
is (Ellipsis, 0, slice(None, None, None))
, or you can create a little helper object:
class ExtendedSliceMaker(object):
def __getitem__(self, idx):
return idx
>>> ExtendedSliceMaker()[...,0,:]
(Ellipsis, 0, slice(None, None, None))
这篇关于Numpy:arr [...,0,:]有效。但是如何存储slice命令(...,0,:)中包含的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!