简体   繁体   English

如何在 Python 3 中实现切片?

[英]How to implement slice in Python 3?

I read something about slice in Python 3. Then I wrote a program, tried to implement __getitem__(self, slice(s)) .我在 Python 3 中阅读了有关 slice 的内容。然后我编写了一个程序,尝试实现__getitem__(self, slice(s)) Code goes below:代码如下:

class NewList:
    def __init__(self, lst):
        print('new list')
        self._list = lst
    def __getitem__(self, x):
        if type(x) is slice:
            return [ self._list[n] for n in range(x.start, x.stop, x.step) ]  #error?
        else:
            return self._list[x]
    ...

nl1 = NewList([1,2,3,4,5])
nl1[1:3]  #error occurs

Then I found out x.step is None , which made range raise an exception.然后我发现x.stepNone ,这使得 range 引发异常。 So, how should I implement the __getitem__ method?那么,我应该如何实现__getitem__方法呢?

In the case where you don't know the length of your object there is an obvious trick to circumvent this mandatory parameter.如果您不知道 object 的长度,则有一个明显的技巧可以规避此强制参数。 For example an infinite sequence's getitem can look like this:例如,无限序列的getitem可能如下所示:

  def __getitem__( self, key ) :
    if isinstance( key, slice ) :
       m = max(key.start, key.stop)
       return [self[ii] for ii in xrange(*key.indices(m+1))]
    elif isinstance( key, int ) :
       #Handle int indices

It will only fail if you don't give start and stop but with checking for None this could be handled too.如果您不给出启动和停止,它只会失败,但检查无,这也可以处理。

If x is a slice, you can do the same as the other condition:如果x是一个切片,你可以像其他条件一样做:

return self._list[x]

You need to use the slice.indices method.您需要使用slice.indices方法。 Given the length of your sequence, it returns a tuple of start, stop, step:给定序列的长度,它会返回一个包含 start、stop、step 的元组:

>>> s = slice(2, 5, None)
>>> s.indices(10)
(2, 5, 1)

>>> [x for x in range(*s.indices(10))]
[2, 3, 4]

>>> s.indices(3)
(2, 3, 1)

>>> s.indices(0)
(0, 0, 1)

how about x.step or 1 ? x.step or 1怎么样?

class NewList:
    def __init__(self, lst):
        print('new list')
        self._list = lst
    def __getitem__(self, x):
        if type(x) is slice:
            return [ self._list[n] for n in range(x.start, x.stop, x.step or 1) ]  #error?
        else:
            return self._list[x]

nl1 = NewList([1,2,3,4,5])
nl1[1:3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM