Python String Slicing Stride Clarification
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"
?The easiest way to explain is probably to address your examples:
"123456"[::-2]
# This takes the whole string ([::])
# Then it works backward (-)
# and it does every other character (2)
"123456"[1::-2]
# This is also working backward (-)
# every other character (2)
# but starting at position 1, which is the number 2.
"123456"[2::-2]
# Again, working backward (-)
# Every other character (2)
# begin at position 2, so you end up with positions 2, and 0, or '31'
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.
Remember that indices start from zero. With this in mind, it may be clearer if you change your string to '012345'
:
In [1]: '012345'[1::-2]
Out[1]: '1'
In [2]: '012345'[2::-2]
Out[2]: '20'
In [3]: '012345'[5::-2]
Out[3]: '531'
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.
That's because the syntax is string[start:end:step]
"123456"[::-2]
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.
"123456"[1::-2]
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.
"123456"[2::-2]
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"
When you're using stride it has basically two parameters. (index to start stride, stride interval)
"1234"[index::interval]
Just remember the stings are indexed starting at zero so
"123456"[2::-2]
is saying that start at index 2 or "3" and grab each within the interval.
"31"
lets understand about the slicing in python syntax:
string[starting index : ending index : step]
and your first example:
"123456"[::-2]
[::]
, 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.
The way I understand this is by asking myself what are the defaults for start
, stop
and step
when they are not specified. Say we want to slice the string x="123456"
. We have
x[start:stop:step]
equivalent to
x[slice(start,stop,step)]
- When
none
is specified
slice is [::]
that is equivalent to:
[0:len(x):1]
Stop being len(x)
as we start index starts at 0
.
- When step is negative
Let's say slice is this: [::-1]
equivalent to:
[-1: -(len(x) + 1) : -1]
=[-1: ~len(x): -1] #~ being the bitwise complement
Let's see if we can answer your questions based upon the defaults.
Case a
>>> "123456"[::-2]
"642"
Say
>>> x = "123456"
>>> x[::-2]
= [-1: -( len(x) + 1) :-2]
= x[-1:-7:-2]
= x[-1] + x[-1 + -2] + x[-1 + -2 + -2]
= x[-1] + x[-3] + x[-5]
= "642"
Case b
>>> "123456"[1::-2]
"2"
Here are the steps to reproduce to get to the result:
>>> x[1::-2]
= x[1: -(len(x) + 1): -2]
= x[1:-7:-2]
= x[1] + x(two indices to the left of x[1])
= x[1] + "" = x[1]
= "2"
Case 3
Here is case 3:
>>> "123456"[2::-2]
"31"
Steps to reproduce:
>>> x[2::-2]
= x[2: -(len(x) + 1): -2]
= x[2:-7:-2]
= x[2] + x[2-2]
= x[2] + x[0]
= "31"
精彩评论