Python 字符串切片步幅说明

发布于 2024-12-09 15:15:41 字数 195 浏览 0 评论 0原文

所以我并没有真正理解切片中的步幅参数。
例如,"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

童话 2024-12-16 15:15:41

最简单的解释方法可能是解决您的示例:

"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'

切片语法为 [::step]。如果省略 并且步骤为负,则它从字符串末尾开始。

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.

我的鱼塘能养鲲 2024-12-16 15:15:41

请记住,索引从零开始。考虑到这一点,如果将字符串更改为 '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'

如您所见,切片确实从正确的索引开始,并且在每个位置都采取正确的(负)步幅步骤直到 in 到达字符串的开头。

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.

就像说晚安 2024-12-16 15:15:41

这是因为语法是 string[start:end:step]

"123456"[::-2]

生成“642”,因为它将从最后一个字符开始。这是因为您没有提供切片将从哪个位置执行。所以它会从最后一个字符向后退 2 个字符,直到到达第一个字符。

"123456"[1::-2]

生成“2”,因为您告诉 Python 从第二个字符(字符串的索引 1)开始,并且告诉 Python 从该位置后退 2 步。在这种情况下,Python 显然只会返回“2”。这是因为当 Python 尝试返回 2 步时,它只用了一步就已经命中了字符串中的第一个字符。

"123456"[2::-2]

现在你应该能够弄清楚这一点了。但无论如何我都会解释一下。因此,您告诉 Python 从第三个字符(或索引 2)开始,然后从那里返回 2 步,直到到达第一个字符。因此,它将从“3”开始,然后返回 2 个步骤,并且意外地到达了第一个字符(恰好是“1”)。所以Python会给你“31”

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"

放低过去 2024-12-16 15:15:41

当您使用步幅时,它基本上有两个参数。 (开始步幅的索引,步幅间隔)

"1234"[index::interval]

只要记住这些刺是从零开始索引的,所以

"123456"[2::-2]

说从索引 2 或“3”开始并抓取间隔内的每个。

"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"
雨后咖啡店 2024-12-16 15:15:41

让我们了解一下 python 语法中的切片:

string[starting index : ending index : step]

以及第一个示例:

"123456"[::-2]

[::],如果不指定任何起始索引,它将采用整个字符串,123456

[::-2]< /code>,值-2表示从字符串末尾开始每2步计数。

所以,你会得到 642。

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.

左岸枫 2024-12-16 15:15:41

我理解这一点的方式是问自己,当未指定 startstopstep 时,它们的默认值是什么。假设我们要对字符串 x="123456" 进行切片。我们

x[start:stop:step]

等效于

x[slice(start,stop,step)]

  1. When none isspecified

slice is [::] 等效于:

[0:len(x):1]

当我们开始索引时停止为 len(x)0 开始。


  1. 当步长为负时

假设切片是这样的: [::-1]

相当于:

[-1: -(len(x) + 1) : -1]
=[-1: ~len(x): -1] #~ being the bitwise complement

让我们看看是否可以根据默认值回答您的问题。


案例 a

>>> "123456"[::-2]
"642"

>>> 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"

案例 b

>>> "123456"[1::-2]
"2"

以下是重现获得结果的步骤:

>>> 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"

案例 3

这是案例 3:

>>> "123456"[2::-2]
"31"

重现步骤:

>>> x[2::-2]
= x[2: -(len(x) + 1): -2]
= x[2:-7:-2]
= x[2] + x[2-2]
= x[2] + x[0]
= "31"

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)]

  1. 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.


  1. 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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文