【发布时间】:2020-02-07 20:04:48
【问题描述】:
所以我做了一个翻译器,它需要一个英文句子,将最后一个字母移到前面,在每个单词后添加 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
只是好奇是否有人可以帮助向我解释我在这里缺少什么
【问题讨论】:
-
for words in sentence不产生单词;它产生单个字符。如果您想要文字,请使用for words in sentence.split() -
是啊,傻我忘了事先拆开它
标签: python python-3.x slice