如何在Python 3中实现切片?

发布于 2024-11-29 20:16:38 字数 564 浏览 3 评论 0原文

我读到了一些关于 Python 3 中 slice 的内容。然后我编写了一个程序,尝试实现 __getitem__(self, slice(s)) 。代码如下:

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

然后我发现x.stepNone,这使得range引发异常。 那么,我应该如何实现__getitem__方法呢?

I read something about slice in Python 3. Then I wrote a program, tried to implement __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.
So, how should I implement the __getitem__ method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷途知返 2024-12-06 20:16:38

您需要使用 slice.indices 方法。给定序列的长度,它返回一个包含开始、停止、步骤的元组:

>>> 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)

You need to use the slice.indices method. Given the length of your sequence, it returns a tuple of 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)
妄想挽回 2024-12-06 20:16:38

在您不知道对象长度的情况下,有一个明显的技巧可以绕过这个强制参数。例如,无限序列的 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

如果您不给出开始和停止,它只会失败,但通过检查 None 这也可以处理。

In the case where you don't know the length of your object there is an obvious trick to circumvent this mandatory parameter. For example an infinite sequence's getitem can look like this:

  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.

烟柳画桥 2024-12-06 20:16:38

如果 x 是一个切片,则可以执行与其他条件相同的操作:

return self._list[x]

If x is a slice, you can do the same as the other condition:

return self._list[x]
梦开始←不甜 2024-12-06 20:16:38

x.step 或 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]

how about 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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文