Python 字符串切片步幅说明
所以我并没有真正理解切片中的步幅参数。
例如,"123456"[::-2]
生成 "642"
,但为什么 "123456"[1::-2]
> 产生 "2"
和 "123456"[2::-2]
产生 "31"
?
So I don't really get the deal with the stride parameter in slicing.
For example, "123456"[::-2]
produces "642"
, but why does "123456"[1::-2]
produce "2"
and "123456"[2::-2]
produce "31"
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
最简单的解释方法可能是解决您的示例:
切片语法为
[::step]
。如果省略
并且步骤为负,则它从字符串末尾开始。The easiest way to explain is probably to address your examples:
The slicing syntax is
[<start>:<end>:step]
. If<start>
is omitted and the step is negative then it starts at the end of the string.请记住,索引从零开始。考虑到这一点,如果将字符串更改为
'012345'
可能会更清楚:如您所见,切片确实从正确的索引开始,并且在每个位置都采取正确的(负)步幅步骤直到 in 到达字符串的开头。
Remember that indices start from zero. With this in mind, it may be clearer if you change your string to
'012345'
:As you can see, the slice does start from the correct index and does take the correct (negative) stride at each step until in reaches the beginning of the string.
这是因为语法是
string[start:end:step]
生成“642”,因为它将从最后一个字符开始。这是因为您没有提供切片将从哪个位置执行。所以它会从最后一个字符向后退 2 个字符,直到到达第一个字符。
生成“2”,因为您告诉 Python 从第二个字符(字符串的索引 1)开始,并且告诉 Python 从该位置后退 2 步。在这种情况下,Python 显然只会返回“2”。这是因为当 Python 尝试返回 2 步时,它只用了一步就已经命中了字符串中的第一个字符。
现在你应该能够弄清楚这一点了。但无论如何我都会解释一下。因此,您告诉 Python 从第三个字符(或索引 2)开始,然后从那里返回 2 步,直到到达第一个字符。因此,它将从“3”开始,然后返回 2 个步骤,并且意外地到达了第一个字符(恰好是“1”)。所以Python会给你“31”
That's because the syntax is
string[start:end:step]
Produces "642" because it'll start from the last character. This is because you didn't provide from which position the slice would be executed. So it'll go back by 2 characters from the last one until it reaches the first one.
Produces "2" because you told Python to start from the 2nd character (index 1 from the string) and you told Python to go back 2 steps from that position. Which in this case, Python would obviously return just "2". This is because when Python tried to go back by 2 steps, it already hit the first character in the string with just one step.
You should be able to figure this one out by now. But I'll just explain it anyway. So, you told Python to start from the third character (or index 2) and then go back 2 steps from there until it reaches the first character. So it'll start from "3" and then go back 2 steps, and accidentally the first character - which happens to be "1" - has been reached. So Python will give you "31"
当您使用步幅时,它基本上有两个参数。 (开始步幅的索引,步幅间隔)
只要记住这些刺是从零开始索引的,所以
说从索引 2 或“3”开始并抓取间隔内的每个。
When you're using stride it has basically two parameters. (index to start stride, stride interval)
Just remember the stings are indexed starting at zero so
is saying that start at index 2 or "3" and grab each within the interval.
让我们了解一下 python 语法中的切片:
以及第一个示例:
[::]
,如果不指定任何起始索引,它将采用整个字符串,123456[::-2]< /code>,值-2表示从字符串末尾开始每2步计数。
所以,你会得到 642。
lets understand about the slicing in python syntax:
and your first example:
[::]
, without specifying any starting index, it will take the whole string, 123456[::-2]
, the value -2 means start counting from end of the string with every 2 steps.So, you will get 642.
我理解这一点的方式是问自己,当未指定
start
、stop
和step
时,它们的默认值是什么。假设我们要对字符串x="123456"
进行切片。我们等效于
none
isspecifiedslice is
[::]
等效于:当我们开始索引时停止为
len(x)
从0
开始。假设切片是这样的:
[::-1]
相当于:
案例 a
说
案例 b
以下是重现获得结果的步骤:
案例 3
这是案例 3:
重现步骤:
The way I understand this is by asking myself what are the defaults for
start
,stop
andstep
when they are not specified. Say we want to slice the stringx="123456"
. We haveequivalent to
none
is specifiedslice is
[::]
that is equivalent to:Stop being
len(x)
as we start index starts at0
.Let's say slice is this:
[::-1]
equivalent to:
Case a
Say
Case b
Here are the steps to reproduce to get to the result:
Case 3
Here is case 3:
Steps to reproduce: