将包含字符串的列表全部转换为小写或大写 [英] Convert a list with strings all to lowercase or uppercase
本文介绍了将包含字符串的列表全部转换为小写或大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含字符串的 Python 列表变量.有没有一种函数可以一次将所有字符串转换为小写,反之亦然,大写?
解决方案
可以通过 列表推导
<预><代码>>>>[x.lower() for x in [A", B", C"]]['a', 'b', 'c']>>>[x.upper() for x in [a", b", c"]]['A', 'B', 'C']或使用 map
函数
I have a Python list variable that contains strings. Is there a function that can convert all the strings in one pass to lowercase and vice versa, uppercase?
解决方案
It can be done with list comprehensions
>>> [x.lower() for x in ["A", "B", "C"]]
['a', 'b', 'c']
>>> [x.upper() for x in ["a", "b", "c"]]
['A', 'B', 'C']
or with the map
function
>>> list(map(lambda x: x.lower(), ["A", "B", "C"]))
['a', 'b', 'c']
>>> list(map(lambda x: x.upper(), ["a", "b", "c"]))
['A', 'B', 'C']
这篇关于将包含字符串的列表全部转换为小写或大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文