Python: 如何反转字符串列表中的每个字符?

Python: How do I reverse every character in a string list?

例如,myList = ['string1', 'string2', 'string3']...现在我希望我的结果是这样的:

 ['1gnirts', '2gnirts', '3gnirts']

我找到了几个关于如何反转字符串的示例,但并不具体针对这个问题...我对如何反转列表中的字符串有点困惑。

reversed_strings = [x[::-1] for x in myList][::-1]

您需要使用两个东西,用于列表的反转函数,以及用于反转字符串的 [::-1]。这可以作为列表理解来完成,如下所示。

myList.reverse
newList = [x[::-1] for x in myList]

如果您已经知道如何反转单个单词,对于本题,您只需对列表中的每个单词做相同的操作即可:

def reverseWord(word):
    # one way to implement the reverse word function
    return word[::-1]

myList = ["string1", "string2", "string3"]

# apply the reverseWord function on each word using a list comprehension
reversedWords = [reverseWord(word) for word in myList]

oneShot = [x[::-1] for x in myList][::-1]