反向字符串: string[::-1] 有效,但 string[0::-1] 和其他人无效 [英] Reverse string: string[::-1] works, but string[0::-1] and others don't

查看:71
本文介绍了反向字符串: string[::-1] 有效,但 string[0::-1] 和其他人无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点像 Python/编程新手,我刚刚在玩字符串切片.因此,string[::-1] 的简单字符串反转方法可以正常工作,但在我下面的代码中还有其他实例会产生意想不到的结果:

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:

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?

推荐答案

切片符号[a:b:c]"的意思是从 a 开始以 c 为增量计数代码> 包含,最多 b 独占".如果 c 为负数,则向后计数,如果省略,则为 1.如果省略 a,则尽可能从计数方向开始(所以如果 c 为正则为开始,如果为负则为结束).如果 b 被省略,那么你会尽可能地朝着你计数的方向结束(所以如果 c 是正数,那就是结束,如果是负数,那就是开始).如果 ab 为负数,则它是从结尾(-1 是最后一个字符)而不是开头的偏移量.

Slice notation "[a:b:c]" means "count in increments of c starting at a inclusive, up to b exclusive". If c is negative you count backwards, if omitted it is 1. If a is omitted then you start as far as possible in the direction you're counting from (so that's the start if c is positive and the end if negative). If b is omitted then you end as far as possible in the direction you're counting to (so that's the end if c is positive and the start if negative). If a or b is negative it's an offset from the end (-1 being the last character) instead of the start.

好的,所以 string[0::-1] 是一个字符,它表示尽可能从索引 0 向后计数".尽可能是字符串的开头.

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:len(string):-1] 或就此而言 string[0:anything:-1] 略有不同.它为空的原因与 string[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[:len(string):-1] 表示从末尾向后计数,但不包括索引 len(string)".无法到达该索引,因此切片为空.

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[:0:-1],但这意味着从末尾向后计数,但不包括索引 0".所以这就是除了第一个字符之外的所有内容,颠倒了.[:0:-1][::-1] 就像 [0:len(string)-1] 是 <代码>[:].在这两种情况下,切片的排除结尾是切片的包含最后一个字符而省略了结尾的索引.

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[-1::-1],它和string[::-1]是一样的,因为-1 表示字符串的最后一个字符.

You also didn't try string[-1::-1], which is the same as string[::-1] because -1 means the last character of the string.

这篇关于反向字符串: string[::-1] 有效,但 string[0::-1] 和其他人无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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