简体   繁体   English

为什么我的字符串反转 my_str[len(my_str)-1:-1:-1] 不产生 output?

[英]Why doesn't my string reversal my_str[len(my_str)-1:-1:-1] produce output?

TL;DR: Why doesn't my_str[6:-1:-1] work while my_str[6::-1] does? TL;DR:为什么my_str[6:-1:-1]不起作用而my_str[6::-1] ] 起作用?

I have a string:我有一个字符串:

my_str="abcdefg"

I have to reverse parts of the string, which I generally do with我必须反转部分字符串,我通常会这样做

my_str[highest_included_index:lowest_included_index-1:-1]

For example,例如,

# returns the reverse of all values between indices 2 and 6 (inclusive)
my_str[6:2-1:-1]
'gfedc'

This pattern breaks down if I want to include the 0 index in my reversal:如果我想在我的反转中包含 0 索引,则此模式会失效:

my_str[6:0-1:-1]  # does NOT work
''
my_str[6:-1:-1]   # also does NOT work
''
my_str[6::-1]     # works as expected
'gfedcba'

Do I have to add an edge case that checks if the lowest index I want to include in a reversed string is zero and not include it in my reversal?我是否必须添加一个边缘案例来检查我想要包含在反转字符串中的最低索引是否为零并且不包含在我的反转中? Ie, do I have to do即,我必须做

for low in range(0,5):
    if low == 0:
        my_str[6::-1]
    else:
        my_str[6:low-1:-1]

That seems... unwieldy and not what I expect from python.这似乎......笨拙,而不是我对 python 的期望。

Edit:编辑:

The closest thing I could find to documentation of this is here :我能找到的最接近文档的地方是:

s[i:j:k]
... ...
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 .步骤 k 从 i 到 j 的 s 切片定义为具有索引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)。 When k is positive, i and j are reduced to len(s) if they are greater.当 k 为正时,如果 i 和 j 更大,则将其缩减为len(s) When k is negative, i and j are reduced to len(s) - 1 if they are greater.当 k 为负时,如果 i 和 j 更大,则将它们减少为len(s) - 1 If i or j are omitted or None , they become “end” values (which end depends on the sign of k).如果 i 或 j 被省略或None ,它们将成为“结束”值(结束取决于 k 的符号)。 Note, k cannot be zero.注意,k 不能为零。 If k is None , it is treated like 1.如果 k 为None ,则将其视为 1。

However, this mentions nothing about a negative j being maximized to zero, which it appears is the current behavior.然而,这并没有提到负 j 被最大化为零,这似乎是当前的行为。

my_str[6:0-1:-1]  # does NOT work
''
my_str[6:-1:-1]   # also does NOT work
''

Yes, they do work -- as documented.是的,它们确实有效——如文件所述。 A negative index is the sequence position, counting from the right.负索引是序列 position,从右数。 These expressions are equivalent to这些表达式等价于

my_str[6:-1:-1]

Since 6 and -1 denote the same position, the result is an empty string.由于6-1表示相同的 position,因此结果为空字符串。 However, if you give a value that is at or past the string start, such as但是,如果您给出一个等于或超过字符串开头的值,例如

my_str[6:-10:-1]

then you see the expected reversal, just as if you'd specified 6::-1然后您会看到预期的反转,就像您指定了6::-1

Yes, you have to make a special case for the discontinuity in indexing.是的,您必须为索引的不连续性做一个特殊情况。

#something like this?
x='my test string'
print(x[3:7][::-1])

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

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