如何在Python中使用切片/切片符号

问题描述 投票:0回答:1

因此,我做了一个翻译,使用英语句子,将最后一个字母移到最前面,在每个单词后添加mu,然后在每三个单词后添加emu。现在,我想做相反的事情。我似乎无法超越现在将单词的第一个字母移到单词末尾的部分。从堆栈溢出帖子How to move the first letter of a word to the end的印象中,我可以通过使用word [1:] + word [0]来完成此操作,但是我尝试实现此功能,但它似乎无能为力。

这是我当前的代码:

sentence = 'imu odmu tnomu emu wknomu whomu otmu emu odmu sthimu'
#This is the result of the english translated sentence

#Get rid of mu and emu 
sentence = sentence.replace('mu', '')
sentence = sentence.replace('e ', '')

#I would like this to move the first letter of each word to the end
print("".join([words[1:] + words[0] for words in sentence]))

#Current output i od tno wkno who ot od sthi
#Expected Output i do not know how to do this

很好奇是否有人可以帮助我向我解释一下我在这里缺少什么

python python-3.x slice
1个回答
2
投票
sentence = 'imu odmu tnomu emu wknomu whomu otmu emu odmu sthimu'
#This is the result of the english translated sentence

#Get rid of mu and emu 
sentence = sentence.replace('mu', '')
sentence = sentence.replace('e ', '')

#I would like this to move the first letter of each word to the end
print(" ".join([words[1:] + words[0] for words in sentence.split()]))
>>> i do not know how to do this
© www.soinside.com 2019 - 2024. All rights reserved.