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

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

问题描述

关于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可能是负数:

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

Slice对象的行为也取决于参数的数量,与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天全站免登陆