理解切片符号 [英] Understanding slice notation

查看:25
本文介绍了理解切片符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对 Python 的切片符号进行很好的解释(参考文献是加分项).

I need a good explanation (references are a plus) on Python's slice notation.

对我来说,这个符号需要一点点掌握.

To me, this notation needs a bit of picking up.

它看起来非常强大,但我还没有完全理解它.

It looks extremely powerful, but I haven't quite got my head around it.

推荐答案

其实很简单:

a[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array

还有 step 值,它可以与以上任何一个一起使用:

There is also the step value, which can be used with any of the above:

a[start:stop:step] # start through not past stop, by step

要记住的关键点是 :stop 值表示所选切片中 not 的第一个值.因此,stopstart 之间的区别在于选择的元素数量(如果 step 为 1,则默认).

The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default).

另一个特点是 startstop 可能是一个 数,这意味着它从数组的末尾开始计数,而不是开始.所以:

The other feature is that start or stop may be a negative number, which means it counts from the end of the array instead of the beginning. So:

a[-1]    # last item in the array
a[-2:]   # last two items in the array
a[:-2]   # everything except the last two items

同理,step 也可以是负数:

Similarly, step may be a negative number:

a[::-1]    # all items in the array, reversed
a[1::-1]   # the first two items, reversed
a[:-3:-1]  # the last two items, reversed
a[-3::-1]  # everything except the last two items, reversed

如果项目数量少于您的要求,Python 对程序员是友好的.例如,如果您要求 a[:-2] 并且 a 只包含一个元素,则会得到一个空列表而不是错误.有时您更喜欢错误,因此您必须意识到这种情况可能会发生.

Python is kind to the programmer if there are fewer items than you ask for. For example, if you ask for a[:-2] and a only contains one element, you get an empty list instead of an error. Sometimes you would prefer the error, so you have to be aware that this may happen.

切片操作符 [] 实际上在上面的代码中使用了 slice() 对象,使用了 : 符号(这是仅在 [] 内有效),即:

The slicing operator [] is actually being used in the above code with a slice() object using the : notation (which is only valid within []), i.e.:

a[start:stop:step]

相当于:

a[slice(start, stop, step)]

根据参数的数量,切片对象的行为也略有不同,类似于 range(),即 slice(stop)slice(start,stop[, step]) 被支持.要跳过指定给定参数,可以使用 None,例如a[start:] 等价于 a[slice(start, None)]a[::-1] 等价于 <代码>a[slice(None, None, -1)].

Slice objects also behave slightly differently depending on the number of arguments, similarly to range(), i.e. both slice(stop) and slice(start, stop[, step]) are supported. To skip specifying a given argument, one might use None, so that e.g. a[start:] is equivalent to a[slice(start, None)] or a[::-1] is equivalent to a[slice(None, None, -1)].

虽然基于 : 的符号对简单切片非常有帮助,但显式使用 slice() 对象简化了切片的程序生成.

While the :-based notation is very helpful for simple slicing, the explicit use of slice() objects simplifies the programmatic generation of slicing.

这篇关于理解切片符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆