如何使用python(手动)反转字符串中的单词?
可能重复:如何使用python(手动)反转字符串中的单词?,python,Python,可能重复: 我知道python已经为此提供了一些方法,但我正在尝试了解这些方法在只有列表数据结构的情况下是如何工作的。如果我有一个字符串hello world,我想创建一个新字符串world hello,我会怎么想 然后,如果我可以用一个新的列表来完成,我如何避免创建一个新的列表并在适当的位置执行它呢?,创建一个新的列表,然后返回零件 str = "hello world" " ".join(str.split()[::-1]) ' '.join(reversed(my_string.spl
我知道python已经为此提供了一些方法,但我正在尝试了解这些方法在只有列表数据结构的情况下是如何工作的。如果我有一个字符串
hello world
,我想创建一个新字符串world hello
,我会怎么想
然后,如果我可以用一个新的列表来完成,我如何避免创建一个新的列表并在适当的位置执行它呢?,创建一个新的列表,然后返回零件
str = "hello world"
" ".join(str.split()[::-1])
' '.join(reversed(my_string.split()))
如果您关心多个空格,请将split()
更改为split(“”)
根据要求,我发布了一个split的实现(由GvR自己从CPython最早的可下载版本的源代码:)
def拆分,空格='\n\t'):
res=[]
i、 n=0,len(s)
而i
我认为现在有更多的python方法(可能是groupby),原始源代码有一个bug(if I=n:
,corrected to=
),创建一个,然后返回部分
' '.join(reversed(my_string.split()))
如果您关心多个空格,请将split()
更改为split(“”)
根据要求,我发布了一个split的实现(由GvR自己从CPython最早的可下载版本的源代码:)
def拆分,空格='\n\t'):
res=[]
i、 n=0,len(s)
而i
我认为现在有更多的python方法(可能是groupby),原始源代码有一个bug(if I=n:
,corrected to=
)您有一个字符串:
str = "A long string to test this algorithm"
拆分字符串(在单词边界处--没有用于拆分的参数):
使用范围或函数反转获得的数组
reversed = splitted[::-1]
用空格连接所有单词,也称为连接
result = " ".join(reversed)
现在,您不需要这么多临时工,将它们组合成一行可以提供:
result = " ".join(str.split()[::-1])
您有一个字符串:
str = "A long string to test this algorithm"
拆分字符串(在单词边界处--没有用于拆分的参数):
使用范围或函数反转获得的数组
reversed = splitted[::-1]
用空格连接所有单词,也称为连接
result = " ".join(reversed)
现在,您不需要这么多临时工,将它们组合成一行可以提供:
result = " ".join(str.split()[::-1])
原始答案
from array import array
def reverse_array(letters, first=0, last=None):
"reverses the letters in an array in-place"
if last is None:
last = len(letters)
last -= 1
while first < last:
letters[first], letters[last] = letters[last], letters[first]
first += 1
last -= 1
def reverse_words(string):
"reverses the words in a string using an array"
words = array('c', string)
reverse_array(words, first=0, last=len(words))
first = last = 0
while first < len(words) and last < len(words):
if words[last] != ' ':
last += 1
continue
reverse_array(words, first, last)
last += 1
first = last
if first < last:
reverse_array(words, first, last=len(words))
return words.tostring()
除了改名,唯一有趣的变化是最后一行。原始答案
from array import array
def reverse_array(letters, first=0, last=None):
"reverses the letters in an array in-place"
if last is None:
last = len(letters)
last -= 1
while first < last:
letters[first], letters[last] = letters[last], letters[first]
first += 1
last -= 1
def reverse_words(string):
"reverses the words in a string using an array"
words = array('c', string)
reverse_array(words, first=0, last=len(words))
first = last = 0
while first < len(words) and last < len(words):
if words[last] != ' ':
last += 1
continue
reverse_array(words, first, last)
last += 1
first = last
if first < last:
reverse_array(words, first, last=len(words))
return words.tostring()
除了重命名,唯一有趣的变化是最后一行。有你的答案和一些Python代码,因为字符串是immutable@JBernardo:可能是他为什么要一个
数组。有你的答案和一些Python代码吗?因为字符串是immutable@JBernardo:可能是他为什么要一个数组
。你能解释一下逻辑吗?此外,正如问题中所指出的,我正在尝试了解仅使用数组进行此操作的基本知识。意思是从str='hello world'
开始,然后创建一个数组并反转单词。字符串通过内置函数拆分为单词。使用[::-1]
切片反转拆分列表,方法与您所问的其他问题和链接副本中所述的方法相同。相反的单词列表然后用空格重新连接起来。请注意“列表”一词。在Python中,数组是完全不同的。您能在这里解释一下逻辑吗?此外,正如问题中所指出的,我正在尝试了解仅使用数组进行此操作的基本知识。意思是从str='hello world'
开始,然后创建一个数组并反转单词。字符串通过内置函数拆分为单词。使用[::-1]
切片反转拆分列表,方法与您所问的其他问题和链接副本中所述的方法相同。相反的单词列表然后用空格重新连接起来。请注意“列表”一词。在Python中,数组是完全不同的。我不明白为什么会被否决。它遍历OP希望澄清的现有答案的步骤,并得出相同的正确结果。@KarlKnechtel:它们都是不错的答案,只是没有回答所问的问题——没有一个使用数组。我不明白为什么会被否决。它遍历OP希望澄清的现有答案的步骤,并得出相同的正确结果。@KarlKnechtel:这些答案都是不错的答案,只是没有回答所问的问题——没有一个答案使用数组。如果我必须对其进行编程,反转是如何工作的?@cfarm reverse返回一个迭代器。您必须将容器从其大小循环到零,这样valueshow才能进行拆分?你能写出split和reversed的代码吗?split通过迭代一个字符串并找到一个分隔符来创建一个列表。你不想自己编写代码(尽管这会很简单),因为这样会太慢,而且会重新发明轮子。如果我必须编程,反转是如何工作的?@cfarm reverse返回一个迭代器。您必须将容器从其大小循环到零,这样valueshow才能进行拆分?你能写出split和reversed的代码吗?split通过迭代一个字符串并找到一个分隔符来创建一个列表。你不想自己编写代码(尽管这很简单),因为这样会太慢,而且会重新发明轮子。单词=数组('c',string)
做什么?@cfarm54:它创建了一个数组