将包含字符串的Python列表转换为小写或大写
我有一个包含字符串的python列表变量。 是否有一个python函数可以将一个传递中的所有字符串转换为小写,反之亦然,大写?
user219126 asked 2019-03-26T09:06:15Z
9个解决方案
311 votes
它可以通过列表推导来完成。 这些基本上采用map的形式。例如,要创建一个新列表,其中所有项目都是较低的(或在第二个片段中加上大写),您将使用:
>>> [() for x in ["A","B","C"]]
['a', 'b', 'c']
>>> [() for x in ["a","b","c"]]
['A', 'B', 'C']
您还可以使用map功能:
>>> map(lambda x:(),["A","B","C"])
['a', 'b', 'c']
>>> map(lambda x:(),["a","b","c"])
['A', 'B', 'C']
YOU answered 2019-03-26T09:06:32Z
42 votes
除了更容易阅读(对于许多人),列表理解也赢得了速度竞赛:
$ python2.6 -m timeit '[() for x in ["A","B","C"]]'
1000000 loops, best of 3: 1.03 usec per loop
$ python2.6 -m timeit '[() for x in ["a","b","c"]]'
1000000 loops, best of 3: 1.04 usec per loop
$ python2.6 -m timeit 'map(,["A","B","C"])'
1000000 loops, best of 3: 1.44 usec per loop
$ python2.6 -m timeit 'map(,["a","b","c"])'
1000000 loops, best of 3: 1.44 usec per loop
$ python2.6 -m timeit 'map(lambda x:(),["A","B","C"])'
1000000 loops, best of 3: 1.87 usec per loop
$ python2.6 -m timeit 'map(lambda x:(),["a","b","c"])'
1000000 loops, best of 3: 1.87 usec per loop
Ned Deily answered 2019-03-26T09:06:57Z
28 votes
>>> map(,["A","B","C"])
['a', 'b', 'c']
ghostdog74 answered 2019-03-26T09:07:14Z
15 votes
列表理解是我如何做到的,它是“Pythonic”的方式。 以下脚本显示如何将列表转换为全部大写然后再转换为低级:
pax@paxbox7:~$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = ["one", "two", "three"] ; x
['one', 'two', 'three']
>>> x = [() for element in x] ; x
['ONE', 'TWO', 'THREE']
>>> x = [() for element in x] ; x
['one', 'two', 'three']
paxdiablo answered 2019-03-26T09:07:39Z
6 votes
对于这个样本,理解是最快的
$ python -m timeit -s 's=["one","two","three"]*1000' '[ for x in s]'
1000 loops, best of 3: 809 usec per loop
$ python -m timeit -s 's=["one","two","three"]*1000' 'map(,s)'
1000 loops, best of 3: 1.12 msec per loop
$ python -m timeit -s 's=["one","two","three"]*1000' 'map(lambda x:(),s)'
1000 loops, best of 3: 1.77 msec per loop
John La Rooy answered 2019-03-26T09:08:03Z
2 votes
mylist = ['Mixed Case One', 'Mixed Case Two', 'Mixed Three']
print map(lambda x: (), mylist)
print map(lambda x: (), mylist)
Chirael answered 2019-03-26T09:08:21Z
1 votes
一个学生问,另一个同样有问题的学生回答:))
fruits=['orange', 'grape', 'kiwi', 'apple', 'mango', 'fig', 'lemon']
newList = []
for fruit in fruits:
(())
print(newlist)
Cristina answered 2019-03-26T09:08:46Z
0 votes
解:
>>> s = []
>>> p = ['This', 'That', 'There', 'is', 'apple']
>>> [(()) if not () else (i) for i in p]
>>> s
>>> ['this', 'that', 'there', 'is','apple']
此解决方案将创建一个包含小写项目的单独列表,无论其原始情况如何。 如果原始案例为高,那么list s将包含list p中相应项目的小写。如果列表项的原始案例在list p中已经是小写,则list s将保留项目的大小写并将其保持为小写。 现在你可以使用list s而不是list p。
Sunil answered 2019-03-26T09:09:16Z
0 votes
如果你的目的是通过一次转换来匹配另一个字符串,你也可以使用。
当你有非ascii字符并与ascii版本匹配时(例如:maßevsmasse),这很有用。虽然或在这种情况下失败,但是()将通过。这在Python 3中可用,并且在回答[/a/31599276/4848659。]中详细讨论了这个想法。
>>>str="Hello World";
>>>print(());
hello world
>>>print(());
HELLO WOLRD
>>>print(());
hello world
Gimhani answered 2019-03-26T09:09:50Z