简体   繁体   English

为什么print(x [0:10:-1])与print(x [::-1])不同?

[英]Why isn't `print(x[0:10:-1])` the same as `print(x[::-1])`?

If you define a string in Python like: 如果您在Python中定义一个字符串,例如:

x = "1234567890"

print(x[::-1]) prints the string in reverse order, but print(x[0:10:-1]) prints nothing. print(x[::-1])以相反的顺序打印字符串,但print(x[0:10:-1])打印任何内容。

Why don't they print the same thing? 他们为什么不打印相同的东西?

See note 5 of the relevant docs : 请参阅相关文档的注释5:

The slice of s from i to j with step k is defined as the sequence of items with index x = i + n*k such that 0 <= n < (ji)/k . S的从i到与步骤k j中的切片被定义为与索引项的序列x = i + n*k ,使得0 <= n < (ji)/k In other words, the indices are i , i+k , i+2*k , i+3*k and so on, stopping when j is reached (but never including j ) 换句话说,索引是ii+ki+2*ki+3*k等,直到达到j时停止(但不包括j

Accordingly, when you specify i = 0, j = 10, k = -1 in [0:10:-1] , there are no values of n which satisfy the equation 0 <= n < (ji)/k , and therefore the range is empty. 因此,当您在[0:10:-1]指定i = 0, j = 10, k = -1时,不存在满足方程0 <= n < (ji)/kn值,因此范围为空。

On the other hand, in [::-1] , i == j == None . 另一方面,在[::-1]i == j == None Refer to the next part of note 5: 请参阅注释5的下一部分:

If i or j are omitted or None, they become “end” values (which end depends on the sign of k ). 如果省略ij或无,则它们将成为“结束”值(该结束取决于k的符号)。

Since k is negative, i becomes len(s) - 1 , which evaluates to 9 . 由于k为负,因此i变为len(s) - 1 ,其值为9 j cannot be represented by any number (since 0 would cut off the last character). j不能用任何数字表示(因为0会截断最后一个字符)。

You should know about the basic rule x[start:stop:step] . 您应该了解基本规则x[start:stop:step] In your code, you put two different rules: print(x[0:10:-1]) and print(x[::-1]) . 在代码中,您放置了两个不同的规则: print(x[0:10:-1])print(x[::-1])

When you put nothing in the start and stop index, your code will be executed using the default depends on the step. 当您在startstop索引中未放置任何内容时,将使用默认值(取决于步骤)来执行代码。 If the step is positive, then the start index will be from 0 and the stop is the length of the string. 如果步长为正,则start索引将从0 startstop为字符串的长度。 Here, you may customize the start and stop value as long as they fulfilling start < stop . 在这里,你可以自定义startstop ,只要他们完成值start < stop On the other hand, if the step is negative, then the start index will be from the last index of the string while the stop is 0 . 另一方面,如果step为负,则start索引将从字符串的最后一个索引开始,而stop则为0 Here, you may also customize the start and stop value as long as they fulfilling start > stop . 在这里,你还可以自定义startstop ,只要价值,因为他们完成start > stop

In your case, you wrote print(x[0:10:-1]) that gives you nothing back. 在您的情况下,您编写了print(x[0:10:-1]) ,但没有任何回报。 This clearly because you set 0 as the start and 10 as the stop index while the step is negative. 显然,这是因为在步骤为负数时将0设置为起始索引,将10为终止索引。

Reference: https://www.digitalocean.com/community/tutorials/how-to-index-and-slice-strings-in-python-3 参考: https//www.digitalocean.com/community/tutorials/how-to-index-and-slice-strings-in-python-3

You are looking for print(x[10:0:-1]) . 您正在寻找print(x[10:0:-1])

>>> x = list(range(10))
>>> x[10:0:-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1]

We can model x[start:end:step] with the following pseudocode: 我们可以使用以下伪代码对x[start:end:step]进行建模:

i = start
if start < end:
  cond = lambda: i < end
else:
  cond = lambda: i > start

while cond():
  SELECT x[i]
  i += step

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM