将所有字符串都转换为小写或大写的Python列表 [英] Convert a Python list with strings all to lowercase or uppercase
本文介绍了将所有字符串都转换为小写或大写的Python列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含字符串的python列表变量.是否有一个python函数可以将所有字符串一次转换为小写,反之亦然,将大写转换为小写?
I have a python list variable that contains strings. Is there a python function that can convert all the strings in one pass to lowercase and vice versa, uppercase?
推荐答案
可以使用列表推导来完成.这些基本上采用[function-of-item for item in some-list]
的形式.例如,要创建一个新列表,其中所有项目均为小写(或在第二个片段中为大写),请使用:
It can be done with list comprehensions. These basically take the form of [function-of-item for item in some-list]
. For example, to create a new list where all the items are lower-cased (or upper-cased in the second snippet), you would use:
>>> [x.lower() for x in ["A","B","C"]]
['a', 'b', 'c']
>>> [x.upper() for x in ["a","b","c"]]
['A', 'B', 'C']
您还可以使用map
功能:
>>> map(lambda x:x.lower(),["A","B","C"])
['a', 'b', 'c']
>>> map(lambda x:x.upper(),["a","b","c"])
['A', 'B', 'C']
这篇关于将所有字符串都转换为小写或大写的Python列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文