在不省略开始和结束切片的情况下反转 Python 字符串
Reverse a Python string without omitting start and end slice
如何在不省略开始和结束切片参数的情况下反转 Python 字符串?
word = "hello"
reversed_word = word[::-1]
我知道这行得通,但是如何通过指定开始和结束索引来获得结果?
word = "hello"
reversed_word = word[?:?:-1]
很难向学生解释为什么 word[::-1]
反转字符串。如果我能给他们逻辑推理而不是 "it's the pythonic way".
会更好
我解释word[::1]
的方式如下:"You have not specified the start so it just starts from the start. You have not specified the end so it just goes until the end. Now the step is 1 so it just goes from the start to the end 1 character by 1."现在当我的学生看到word[::-1]
他们会想"We have not specified the start or the end so it will go through the string -1 characters at a time?"
如果您没有为切片表示法指定数字,Python 将使用 None
作为默认值。因此,您可以直接写出 None
值:
>>> word = "hello"
>>> word[None:None:-1]
'olleh'
>>>
递归方法:
def reverse(word):
if len(word) <= 1:
return word
return reverse(word[1:]) + word[0]
不太清楚为什么,但以下将 return 与 word
相反:
word = "hello"
word[len(word):-(len(word)+1):-1]
或者...
word = "hello"
word[len(word):-len(word)-1:-1]
编辑(解释):
来自杰德沃德的评论:
The middle parameter is the trickiest, but it's pretty straightforward
once you realize (a) negative indices to slice start/stop indicate
that you want to count "backwards" from the end of the string, and (b)
the stop index is exclusive, so it will "count" up to but stop
at/before the stop index. word[len(word):-len(word)-1:-1] is probably
more clear.
回应评论:
第三个值实际上是增量,所以你告诉 Python 你想从最后一个字母开始然后 return 所有 (-1)st 值直到最后一个。
这是一张图(在一分钟内拼凑而成):
图中显示我们也可以这样代替:
word = "hello"
word[-1:-len(word)-1:-1] #-1 as the first
In [27]: word
Out[27]: 'hello'
In [28]: word[-1:-6:-1]
Out[28]: 'olleh'
为什么这有效:
In [46]: word
Out[46]: 'hello'
In [47]: word[-1] # you want o to be your first value
Out[47]: 'o'
In [48]: word[-5] # you want h to be your last value
Out[48]: 'h'
In [49]: word[-1:-6:-1] # therefore ending point should be one
Out[49]: 'olleh' # past your last value that is -6
反转字符串的其他一些方法:
word = "hello"
reversed_word1 = word[-1: :-1]
reversed_word2 = word[len(word)-1: :-1]
reversed_word3 = word[:-len(word)-1 :-1]
关于切片符号 a[i:j:k]
你应该注意的一件事是 省略 i
和 j
并不总是意味着 i
会变为 0
,j
将变为 len(s)
。 It depends upon the sign of k
。默认情况下 k
是 +1
。
- 如果
k
是+ve那么i
的默认值是0
(从头开始)。如果是 -ve 那么 i
的默认值是 -1
(从末尾开始)。
- 如果
k
是 +ve 那么 j
的默认值是 len(s)
(在最后停止)。如果是 -ve 那么 j
的默认值是 -(len(s)+1)
(停在开头)。
现在您可以向学生解释 Hello[::-1]
如何打印 olleH
。
符号 string[begin:end:increment]
可以用以下 Python 程序描述:
def myreverse( string, begin, end, increment ):
newstring = ""
if begin >= 0 and end < 0:
begin = -1
for i in xrange(begin, end, increment):
try:
newstring += string[i]
except IndexError:
pass
return newstring
从Python 2 source开始,这是用三元表达式定义的:
defstart = *step < 0 ? length-1 : 0;
defstop = *step < 0 ? -1 : length;
所以,当没有给出开始和停止时,
如果step为负:
- 开始是长度 - 1
- stop为-1,(这是C索引,在Python中很难实现,必须是-length-1)
如果步长为正:
- 开始是 0
- 停止是长度
所以要回答这个问题:
How would I get the result by specifying the start and end indexes?
要自己指定,请使用例如以下(放入函数中以实现可重用性)
def my_slice(word, step):
'''slice word with only step'''
start = len(word)-1 if step < 0 else 0
stop = -len(word)-1 if step < 0 else len(word)
return word[start:stop:step]
word = "hello"
step = -1
my_slice(word, step)
returns
'olleh'
如何在不省略开始和结束切片参数的情况下反转 Python 字符串?
word = "hello"
reversed_word = word[::-1]
我知道这行得通,但是如何通过指定开始和结束索引来获得结果?
word = "hello"
reversed_word = word[?:?:-1]
很难向学生解释为什么 word[::-1]
反转字符串。如果我能给他们逻辑推理而不是 "it's the pythonic way".
我解释word[::1]
的方式如下:"You have not specified the start so it just starts from the start. You have not specified the end so it just goes until the end. Now the step is 1 so it just goes from the start to the end 1 character by 1."现在当我的学生看到word[::-1]
他们会想"We have not specified the start or the end so it will go through the string -1 characters at a time?"
如果您没有为切片表示法指定数字,Python 将使用 None
作为默认值。因此,您可以直接写出 None
值:
>>> word = "hello"
>>> word[None:None:-1]
'olleh'
>>>
递归方法:
def reverse(word):
if len(word) <= 1:
return word
return reverse(word[1:]) + word[0]
不太清楚为什么,但以下将 return 与 word
相反:
word = "hello"
word[len(word):-(len(word)+1):-1]
或者...
word = "hello"
word[len(word):-len(word)-1:-1]
编辑(解释):
来自杰德沃德的评论:
The middle parameter is the trickiest, but it's pretty straightforward once you realize (a) negative indices to slice start/stop indicate that you want to count "backwards" from the end of the string, and (b) the stop index is exclusive, so it will "count" up to but stop at/before the stop index. word[len(word):-len(word)-1:-1] is probably more clear.
回应
第三个值实际上是增量,所以你告诉 Python 你想从最后一个字母开始然后 return 所有 (-1)st 值直到最后一个。
这是一张图(在一分钟内拼凑而成):
图中显示我们也可以这样代替:
word = "hello"
word[-1:-len(word)-1:-1] #-1 as the first
In [27]: word
Out[27]: 'hello'
In [28]: word[-1:-6:-1]
Out[28]: 'olleh'
为什么这有效:
In [46]: word
Out[46]: 'hello'
In [47]: word[-1] # you want o to be your first value
Out[47]: 'o'
In [48]: word[-5] # you want h to be your last value
Out[48]: 'h'
In [49]: word[-1:-6:-1] # therefore ending point should be one
Out[49]: 'olleh' # past your last value that is -6
反转字符串的其他一些方法:
word = "hello"
reversed_word1 = word[-1: :-1]
reversed_word2 = word[len(word)-1: :-1]
reversed_word3 = word[:-len(word)-1 :-1]
关于切片符号 a[i:j:k]
你应该注意的一件事是 省略 i
和 j
并不总是意味着 i
会变为 0
,j
将变为 len(s)
。 It depends upon the sign of k
。默认情况下 k
是 +1
。
- 如果
k
是+ve那么i
的默认值是0
(从头开始)。如果是 -ve 那么i
的默认值是-1
(从末尾开始)。 - 如果
k
是 +ve 那么j
的默认值是len(s)
(在最后停止)。如果是 -ve 那么j
的默认值是-(len(s)+1)
(停在开头)。
现在您可以向学生解释 Hello[::-1]
如何打印 olleH
。
符号 string[begin:end:increment]
可以用以下 Python 程序描述:
def myreverse( string, begin, end, increment ):
newstring = ""
if begin >= 0 and end < 0:
begin = -1
for i in xrange(begin, end, increment):
try:
newstring += string[i]
except IndexError:
pass
return newstring
从Python 2 source开始,这是用三元表达式定义的:
defstart = *step < 0 ? length-1 : 0; defstop = *step < 0 ? -1 : length;
所以,当没有给出开始和停止时,
如果step为负:
- 开始是长度 - 1
- stop为-1,(这是C索引,在Python中很难实现,必须是-length-1)
如果步长为正:
- 开始是 0
- 停止是长度
所以要回答这个问题:
How would I get the result by specifying the start and end indexes?
要自己指定,请使用例如以下(放入函数中以实现可重用性)
def my_slice(word, step):
'''slice word with only step'''
start = len(word)-1 if step < 0 else 0
stop = -len(word)-1 if step < 0 else len(word)
return word[start:stop:step]
word = "hello"
step = -1
my_slice(word, step)
returns
'olleh'