简体   繁体   English

python3,如何反转句子中单词的每个字母

[英]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. 但是单词之间没有空格。

Use split to get the words then loop over and reverse each word and then join them. 使用split获得单词,然后遍历并反转每个单词,然后join它们join

>>> s = "I love you"
>>> " ".join(word[::-1] for word in s.split())
'I evol uoy'

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: 要解决的重要问题是:

  1. Simply calling " ".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 . cx的副本,并且反转它不会更改x
  2. " ".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.

相关问题 Python:混合每个单词的字母并反转 - Python: mix the letter of each word and reverse 如何反转字符串中的每个单词,并且python中每个单词的第一个字母大写? - How to reverse each word in string and first letter is capitalize of each word in python? 如何使用循环而不是方法获取句子字符串中每个单词大写的第一个字母? 在 python - How to get the 1st letter of each word capital in a string of sentence using loops but not methods? in python 当它在整个句子上调用反向然后在每个单词上反向时,如何解决句子O(N)中的反向单词? - How is this solution to reversing words in a sentence O(N) when it calls reverse on the whole sentence and then reverse on each word? 将句子/单词的每个字母更改为特定的内容 - Change each letter of the sentence/word to something specific 如何获取单词列表的每个字母 python - How to get each letter of word list python 如何将 python 中每个单词的首字母大写? - How to capitalize the First letter of each word in python? 用前一个单词的字母反向替换每个单词的第一个字母 - Reverse-replacing the first letter of each word with the letter of the previous word Python在没有内置函数的情况下反转句子中的每个单词python同时保留顺序 - Python reverse each word in a sentence without inbuilt function python while preserve order 用Python将每个句子的首字母大写 - Capitalizing the first letter of each sentence in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM