[英]python3, how to reverse each letter of a word in a sentence
s="I love you"
how can I get "I evol uoy"
? 如何获得"I evol uoy"
?
I tried... 我试过了...
words=s.split()
for x in words:
c=list(x)
c.reverse()
" ".join(c)
and I get... 我得到...
'I'
'evol'
'uoy'
I again tried... 我再次尝试...
words=s.split()
s1=[]
for x in words:
c=list(x)
c.reverse()
for y in c:
s1.append(y)
Now s1
is: 现在s1
是:
['I', 'e', 'v', 'o', 'l', 'u', 'o', 'y']
but there is no space between words. 但是单词之间没有空格。
Here's how to fix your first solution: 以下是解决第一个解决方案的方法:
s = "I love you"
words = s.split()
def reverse_word(x):
c = list(x)
c.reverse()
return "".join(c)
result = " ".join(reverse_word(w) for w in words)
print(result)
The important things to fix are that: 要解决的重要问题是:
" ".join(c)
without making use of it somewhere doesn't help. 简单地调用" ".join(c)
而不在某处使用它没有帮助。 c
is a copy of x
, and reversing it doesn't change x
. c
是x
的副本,并且反转它不会更改x
。 " ".join(c)
is wrong anyway because you don't want a space between letters, so it's now "".join(c)
. " ".join(c)
还是错误的,因为您不希望字母之间有空格,所以现在是"".join(c)
。 The spaces are added to the actual reversed sentence. 空格将添加到实际的反向句子中。 And the second one: 第二个:
s1 = []
for x in words:
new_word = []
c = list(x)
c.reverse()
for y in c:
new_word.append(y)
s1.append("".join(new_word))
result = " ".join(s1)
The important difference here is that you need a list for each word (consisting of letters) as well as for the whole sentence (consisting of words), since as you saw a single list for both doesn't work. 这里的重要区别是,您需要为每个单词(由字母组成)以及整个句子(由单词组成)提供一个列表,因为您看到的两个列表都无法使用。 I've kept it similar to your code but the new_word
is actually redundant since it's just an exact copy of c
. 我将其保留为类似于您的代码,但new_word
实际上是多余的,因为它只是c
的精确副本。 So it could be simplified as follows: 因此可以简化如下:
s1 = []
for x in words:
c = list(x)
c.reverse()
s1.append("".join(c))
result = " ".join(s1)
This is an extremely common pattern: create an empty list, append some element to it in each iteration of a for loop, and then keep the final result. 这是一种非常常见的模式:创建一个空列表,在for循环的每次迭代中向其添加一些元素,然后保留最终结果。 It's so common that Python has a special syntax for it called list comprehensions: Python非常普遍,它具有一种特殊的语法,称为列表推导:
def reverse_word(x):
c = list(x)
c.reverse()
return "".join(c)
s1 = [reverse_word(word) for word in words]
This is the more elegant, Pythonic way. 这是更优雅的Python方式。 In fact you'll notice now that the first bit of code above is almost exactly this, except that I immediately used s1
without even naming it, and there are no square brackets because now it's a generator comprehension and a nice little feature of Python's syntax means that just the parentheses are valid since join
only takes a single argument. 实际上,您现在会注意到上面的代码几乎完全是这样,除了我没有命名就立即使用s1
,并且没有方括号,因为它现在是生成器理解和Python语法的一个不错的小功能表示仅括号有效,因为join
仅接受单个参数。 But now we're getting deep, don't worry too much. 但是现在我们越来越深入,不必担心太多。
Finally reverse_word(word)
can simply be replaced by word[s::-1]
using Python's slice notation, and everything can be done in one step, and that's the solution given by AKS. 最后,使用Python的切片符号可以简单地用word[s::-1]
替换reverse_word(word)
,并且所有操作都可以一步完成,这就是AKS提供的解决方案。
将字符串拆分为单词,反转每个单词,然后将其加入。
' '.join(i[::-1] for i in s.split())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.