Python:如何反转字符串列表中的每个字符? [英] Python: How do I reverse every character in a string list?

查看:116
本文介绍了Python:如何反转字符串列表中的每个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For instance, myList = ['string1', 'string2', 'string3']... now I want my result to be this:

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

我发现了一些关于如何反向转换字符串的示例,但并非特定于此问题……我对如何反向转换列表中的字符串感到有些困惑.

I've found few examples on how to reverse strings in general but not specific to this problem ... I'm bit confused on how to do a reverse of strings that are in a list.

推荐答案

如果您已经知道如何反转单个单词,那么对于此问题,您只需对列表中的每个单词都执行相同的操作即可:

If you already know how to reverse a single word, for this problem you only have to do the same for each of the words of the list:

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]

这篇关于Python:如何反转字符串列表中的每个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆