Python 反向字符串: string[::-1] 有效,但 string[0::-1] 和其他人无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21617586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reverse string: string[::-1] works, but string[0::-1] and others don't
提问by Darren Haynes
I am somewhat of a python/programming newbie, and I have just been playing around with string slicing. So the simple string reverse method of string[::-1]works just fine as we know, but there are other instances in my code below that yields unexpected results:
我有点像 python/编程新手,我刚刚在玩字符串切片。因此,string[::-1]正如我们所知,简单的 string reverse 方法工作得很好,但是下面的代码中还有其他实例会产生意想不到的结果:
In [1]: string = "Howdy doody"
In [2]: string[::]
Out[2]: 'Howdy doody'
In [3]: string[::-1]
Out[3]: 'ydood ydwoH'
In [4]: string[0:]
Out[4]: 'Howdy doody'
In [5]: string[0::-1]
Out[5]: 'H' # what up with this?
In [6]: string[:len(string)]
Out[6]: 'Howdy doody'
In [7]: string[:len(string):-1]
Out[7]: '' # what up with this too?
In [8]: string[0:len(string)]
Out[8]: 'Howdy doody'
In [9]: string[0:len(string):-1]
Out[9]: '' # And what up here too.
I have commented on the lines above where I expected the strings to be reversed, but I am surprised to see why they don't simply reverse the string. Anyone know what is up with that?
我已经评论了上面我期望字符串被反转的行,但我很惊讶地看到为什么他们不简单地反转字符串。有谁知道这是怎么回事?
采纳答案by Steve Jessop
Slice notation "[a:b:c]" means "count in increments of cstarting at ainclusive, up to bexclusive". If cis negative you count backwards, if omitted it is 1. If ais omitted then you start as far as possible in the direction you're counting from (so that's the start if cis positive and the end if negative). If bis omitted then you end as far as possible in the direction you're counting to (so that's the end if cis positive and the start if negative). If aor bis negative it's an offset from the end (-1 being the last character) instead of the start.
切片符号“[a:b:c]”的意思是“c以a包含开始的增量计数,直到b独占”。如果c是负数,则向后计数,如果省略,则为 1。如果a省略,则尽可能从您开始计数的方向开始(如果c为正,则为结束,如果为负,则为结束)。如果b被省略,那么你尽可能地在你计数的方向上结束(所以如果c是正数则结束,如果是负数则开始)。如果a或b为负数,则它是从结尾(-1 是最后一个字符)而不是开头的偏移量。
OK, so string[0::-1]is one character, it says "count backwards from index 0 as far as you can". As far as it can is the start of the string.
好的,string[0::-1]一个字符也是如此,它说“尽可能从索引 0 向后计数”。尽可能是字符串的开始。
string[0:len(string):-1]or for that matter string[0:anything:-1]is subtly different. It's empty for the same reason that string[1:0]is empty. The designated end of the slice cannot be reached from the start. You can think of this as the slice having ended "before" it began (hence is empty), or you can think of the end point being automatically adjusted to be equal to the start point (hence the slice is empty).
string[0:len(string):-1]或者就此而言string[0:anything:-1]是微妙的不同。它是空的,原因与空的相同string[1:0]。从头开始无法到达指定的切片末端。您可以将其视为切片在它开始“之前”结束(因此为空),或者您可以将终点视为自动调整为等于起点(因此切片为空)。
string[:len(string):-1]means "count backwards from the end up to but not including index len(string)". That index can't be reached, so the slice is empty.
string[:len(string):-1]表示“从末尾向后计数,但不包括索引len(string)”。无法到达该索引,因此切片为空。
You didn't try string[:0:-1], but that means "count backwards from the end up to but not including index 0". So that's all except the first character, reversed. [:0:-1]is to [::-1]as [0:len(string)-1]is to [:]. In both cases the excluded end of the slice is the index that would have been the included last character of the slice with the end omitted.
您没有尝试string[:0:-1],但这意味着“从末尾向后计数,但不包括索引 0”。所以这就是除了第一个字符之外的所有内容,颠倒了。[:0:-1]是[::-1]为[0:len(string)-1]是[:]。在这两种情况下,被排除的切片的结尾是切片的包含最后一个字符而省略了结尾的索引。
You also didn't try string[-1::-1], which is the same as string[::-1]because -1means the last character of the string.
您也没有尝试string[-1::-1],这与string[::-1]因为-1表示字符串的最后一个字符相同。
回答by mhlester
In all of your cases which aren't working, you're startingat the beginningand then moving backwards. There is no further backwards than the beginning, so you don't get any more characters.
在所有的情况下,这是不工作,你开始的开始,然后移动倒退。没有比开始更倒退的地方了,所以你不会得到更多的字符。
This would be the solution:
这将是解决方案:
>>> string[len(string)::-1]
'ydood ydwoH'
The slice notation is start, end, step. Where stepis negative, startshould be greater than endto get anything. Where it's left blank ([::-1]), that nuance is followed automatically
切片符号是start, end, step。哪里step是负数,start应该大于end得到什么。留空 ( [::-1]) 时,会自动遵循细微差别
In the case where you only got 'H', it can be confusing why there was anything. But think of the explanation written out:
在你只得到的情况下'H',可能会混淆为什么有任何东西。但是想想写出来的解释:
Beginning with the character at start, increment by stepuntil (but not including) end
与字符在开始启动,递增由步骤直到(但不包括)端
Now it becomes clear that because you start with 0, the 0 character is included
现在很清楚,因为您从 0 开始,所以包含了 0 字符

