什么是默认切片索引*真的*? [英] What are the default slice indices *really*?

查看:65
本文介绍了什么是默认切片索引*真的*?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 python 文档docs.python.org/tutorial/introduction.html#strings:

<块引用>

切片索引有有用的默认值;省略的第一个索引默认为零,省略的第二个索引默认为被切片的字符串的大小.

对于标准情况,这很有意义:

<预><代码>>>>s = '我的字符串'>>>s[1:]'ystring'>>>s[:3]'我的'>>>s[:-2]'密斯特里'>>>s[-1:]'G'>>>

到目前为止,一切都很好.但是,使用负步长值似乎暗示了略有不同的默认值:

<预><代码>>>>s[:3:-1]'格尼尔'>>>s[0:3:-1]''>>>s[2::-1]'符号'

好吧,也许如果步长为负,则默认值相反.省略的第一个索引默认为被切片的字符串的大小,省略的第二个索引默认为零:

<预><代码>>>>s[len(s):3:-1]'格尼尔'

看起来不错!

<预><代码>>>>s[2:0:-1]'s'

哎呀.错过了那个'm'.

然后就是大家最喜欢的字符串逆向语句了.甜蜜的是:

<预><代码>>>>s[::-1]'gnirtsym'

然而:

<预><代码>>>>s[len(s):0:-1]'gnirtsy'

切片从不包含切片中第二个索引的值.我可以看到这样做的一致性.

所以我想我开始了解 slice 在其各种排列中的行为.但是,我感觉第二个索引有点特殊,负步的第二个索引的默认值实际上不能用数字来定义.

谁能简明地定义可以解释所提供示例的默认切片索引?文档将是一个巨大的优势.

解决方案

实际上没有任何默认值;省略的值会被特殊处理.

然而,在每种情况下,遗漏值的处理方式与 None 完全相同.这意味着,除非您正在破解解释器(或使用 parserast 等模块),否则您可以假装默认值为 None(作为递归的answer 说),你总会得到正确的答案.

引用的非正式文档不是很准确——这对于本应成为教程一部分的内容来说是合理的.要获得真正的答案,您必须查阅参考文档.

对于 2.7.3,序列类型 在注释 3、4 和 5 中描述了切片.

对于[i:j]:

<块引用>

... 如果省略 iNone,则使用 0.如果省略 jNone,请使用 len(s).

对于[i:j:k]:

<块引用>

如果 ij 被省略或 None,它们将成为结束"值(结束取决于 k).注意,k 不能为零.如果 kNone,则将其视为 1.

对于 3.3,序列类型 与 2.7.3 的措辞完全相同.

From the python documentation docs.python.org/tutorial/introduction.html#strings:

Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.

For the standard case, this makes a lot of sense:

>>> s = 'mystring'
>>> s[1:]
'ystring'
>>> s[:3]
'mys'
>>> s[:-2]
'mystri'
>>> s[-1:]
'g'
>>> 

So far, so good. However, using a negative step value seems to suggest slightly different defaults:

>>> s[:3:-1]
'gnir'
>>> s[0:3:-1]
''
>>> s[2::-1]
'sym'

Fine, perhaps if the step is negative, the defaults reverse. An ommitted first index defaults to the size of the string being sliced, an omitted second index defaults to zero:

>>> s[len(s):3:-1]
'gnir'

Looking good!

>>> s[2:0:-1]
'sy'

Whoops. Missed that 'm'.

Then there is everyone's favorite string reverse statement. And sweet it is:

>>> s[::-1]
'gnirtsym'

However:

>>> s[len(s):0:-1]
'gnirtsy'

The slice never includes the value of the second index in the slice. I can see the consistency of doing it that way.

So I think I am beginning to understand the behavior of slice in its various permutations. However, I get the feeling that the second index is somewhat special, and that the default value of the second index for a negative step can not actually be defined in terms of a number.

Can anyone concisely define the default slice indices that can account for the provided examples? Documentation would be a huge plus.

解决方案

There actually aren't any defaults; omitted values are treated specially.

However, in every case, omitted values happen to be treated in exactly the same way as None. This means that, unless you're hacking the interpreter (or using the parser, ast, etc. modules), you can just pretend that the defaults are None (as recursive's answer says), and you'll always get the right answers.

The informal documentation cited isn't quite accurate—which is reasonable for something that's meant to be part of a tutorial. For the real answers, you have to turn to the reference documentation.

For 2.7.3, Sequence Types describes slicing in notes 3, 4, and 5.

For [i:j]:

… If i is omitted or None, use 0. If j is omitted or None, use len(s).

And for [i:j:k]:

If i or j are omitted or None, they become "end" values (which end depends on the sign of k). Note, k cannot be zero. If k is None, it is treated like 1.

For 3.3, Sequence Types has the exact same wording as 2.7.3.

这篇关于什么是默认切片索引*真的*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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