Python 在2+字字符串中交换字母
假设我有一个单字字符串Hello,我想交换第一个和最后一个字母,所以我要这样做:Python 在2+字字符串中交换字母,python,string,list,Python,String,List,假设我有一个单字字符串Hello,我想交换第一个和最后一个字母,所以我要这样做: s="Hello" l=list(s) l[0],l[len(l)-1]=l[len(l)-1],l[0] print("".join(l)) 但是,如果我必须交换字符串中每个单词的第一个和最后一个字母:Hello World,这样我就可以得到oellH dorlW了 我曾考虑过使用嵌套列表,但它似乎过于复杂。字符串是不可变的,因此您可以通过切片创建一个新列表: s = "Hello" >>>
s="Hello"
l=list(s)
l[0],l[len(l)-1]=l[len(l)-1],l[0]
print("".join(l))
但是,如果我必须交换字符串中每个单词的第一个和最后一个字母:Hello World,这样我就可以得到oellH dorlW了
我曾考虑过使用嵌套列表,但它似乎过于复杂。字符串是不可变的,因此您可以通过切片创建一个新列表:
s = "Hello"
>>> s[-1] + s[1:-1] + s[0]
"oellH"
要执行多个单词,请按如下方式拆分和重新联接:
s= "Hello World"
>>> ' '.join(word[-1] + word[1:-1] + word[0] for word in s.split())
'oellH dorlW'
字符串是不可变的,因此可以通过切片创建新字符串:
s = "Hello"
>>> s[-1] + s[1:-1] + s[0]
"oellH"
要执行多个单词,请按如下方式拆分和重新联接:
s= "Hello World"
>>> ' '.join(word[-1] + word[1:-1] + word[0] for word in s.split())
'oellH dorlW'
您可以拆分字符串,交换每个单词的字母,然后将其重新连接在一起:
# example is wrong, does not swap, only puts first in the back. see below for fix
text = ' '.join( t[1:]+t[0] for t in "Hello World".split() )
print (text)
输出:
elloH orldW
oellH dorlW
这使用list comprehensionst提取每个拆分的单词t-list切片将前字母移到后面t[1:]+t[0]和“”。join可使字符串列表变回字符串
链接:
和
对于列表,字符串是类似的
它也适用于较长的字符串:
elloH orldW si a eallyr verusedo trings ermt - orF ureS !
正如@cumulation所指出的,我误读了这个问题——我的示例只是将第一个字母放在字符串的末尾,这只会将交换第一个和最后一个字母所做的工作减半:
# t[-1] is the last character put to the front,
# followed by t[1:-1] 1st to (but not including) the last character
# followed by t[0] the first character
text = ' '.join( t[-1]+t[1:-1]+t[0] for t in "Hello World".split() )
print (text)
输出:
elloH orldW
oellH dorlW
您可以拆分字符串,交换每个单词的字母,然后将其重新连接在一起:
# example is wrong, does not swap, only puts first in the back. see below for fix
text = ' '.join( t[1:]+t[0] for t in "Hello World".split() )
print (text)
输出:
elloH orldW
oellH dorlW
这使用list comprehensionst提取每个拆分的单词t-list切片将前字母移到后面t[1:]+t[0]和“”。join可使字符串列表变回字符串
链接:
和
对于列表,字符串是类似的
它也适用于较长的字符串:
elloH orldW si a eallyr verusedo trings ermt - orF ureS !
正如@cumulation所指出的,我误读了这个问题——我的示例只是将第一个字母放在字符串的末尾,这只会将交换第一个和最后一个字母所做的工作减半:
# t[-1] is the last character put to the front,
# followed by t[1:-1] 1st to (but not including) the last character
# followed by t[0] the first character
text = ' '.join( t[-1]+t[1:-1]+t[0] for t in "Hello World".split() )
print (text)
输出:
elloH orldW
oellH dorlW
通过在空格字符上拆分来列出单词
words = string.split(" ")
然后用脚本在该列表上迭代
for word in words:
l = list(word)
l[0], l[len(l) - 1] = l[len(l) - 1], l[0]
print("".join(l))
通过在空格字符上拆分来列出单词
words = string.split(" ")
然后用脚本在该列表上迭代
for word in words:
l = list(word)
l[0], l[len(l) - 1] = l[len(l) - 1], l[0]
print("".join(l))
优雅的肯定会用几句话来表达javaop想要的是oellH dorlW,而不是elloH Worldw。是的,我其实在寻找一些不同的东西,但我完全理解你的逻辑,谢谢你的参考。他的例子与问题的措辞完全一致。即使没有示例,我也会将第一个和最后一个字母解释为final_string[first],final_string[last=initial_string[last],initial_string[first]。也许你应该在你的假设列表中加上你对掉期的理解不正确的可能性。@accumulation-修正了它,误读了它-感谢你让我犹豫了:优雅!肯定会用几行来表达javaop想要的是ellh dorlW,而不是elloH orldW。是的,我实际上在寻找一些不同的东西但我完全理解你的逻辑,谢谢你的参考。他的例子与问题的措辞完全一致。即使没有这个例子,我也会将第一个和最后一个字母解释为final_string[first],final_string[last=initial_string[last],initial_string[first]。也许你应该在你的假设列表中加上你对交换的理解不正确的可能性。@accumulation-修正了它,误读了它-谢谢你把我犹豫的鼻子放进去:好吧,你试过什么了?你怎么把一个句子的词分开?好吧,你试过什么了?你怎么把一个句子的词分开?