gpt4 book ai didi

python - 将包含字符串的列表全部转换为小写或大写

转载 作者:IT老高 更新时间:2023-10-28 12:14:10 25 4
gpt4 key购买 nike

我有一个包含字符串的 Python 列表变量。是否有一个函数可以一次将所有字符串转换为小写,反之亦然,大写?

最佳答案

可以通过 list comprehensions 来完成

>>> [x.lower() for x in ["A", "B", "C"]]
['a', 'b', 'c']
>>> [x.upper() for x in ["a", "b", "c"]]
['A', 'B', 'C']

或使用 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']

关于python - 将包含字符串的列表全部转换为小写或大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1801668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com