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


这可以通过列表推导来完成

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

或者使用映射函数

>>> 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”的方式。下面的文字记录展示了如何将一个列表全部转换为大写,然后再转换回小写:

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 = [element.upper() for element in x] ; x
['ONE', 'TWO', 'THREE']

>>> x = [element.lower() for element in x] ; x
['one', 'two', 'three']

mylist = ['Mixed Case One', 'Mixed Case Two', 'Mixed Three']
print(list(map(lambda x: x.lower(), mylist)))
print(list(map(lambda x: x.upper(), mylist)))

>>> list(map(str.lower,["A","B","C"]))
['a', 'b', 'c']

除了更容易阅读(对许多人来说),列表推导式也在速度竞赛中获胜:

$ python2.6 -m timeit '[x.lower() for x in ["A","B","C"]]'
1000000 loops, best of 3: 1.03 usec per loop
$ python2.6 -m timeit '[x.upper() for x in ["a","b","c"]]'
1000000 loops, best of 3: 1.04 usec per loop

$ python2.6 -m timeit 'map(str.lower,["A","B","C"])'
1000000 loops, best of 3: 1.44 usec per loop
$ python2.6 -m timeit 'map(str.upper,["a","b","c"])'
1000000 loops, best of 3: 1.44 usec per loop

$ python2.6 -m timeit 'map(lambda x:x.lower(),["A","B","C"])'
1000000 loops, best of 3: 1.87 usec per loop
$ python2.6 -m timeit 'map(lambda x:x.upper(),["a","b","c"])'
1000000 loops, best of 3: 1.87 usec per loop

对于这个例子,理解是最快的

$ python -m timeit -s 's=["one","two","three"]*1000' '[x.upper for x in s]'
1000 loops, best of 3: 809 usec per loop

$ python -m timeit -s 's=["one","two","three"]*1000' 'map(str.upper,s)'
1000 loops, best of 3: 1.12 msec per loop

$ python -m timeit -s 's=["one","two","three"]*1000' 'map(lambda x:x.upper(),s)'
1000 loops, best of 3: 1.77 msec per loop

一个学生问,另一个有同样问题的学生回答:)

fruits=['orange', 'grape', 'kiwi', 'apple', 'mango', 'fig', 'lemon']
newList = []
for fruit in fruits:
    newList.append(fruit.upper())
print(newList)

解决方案:

>>> s = []
>>> p = ['This', 'That', 'There', 'is', 'apple']
>>> [s.append(i.lower()) if not i.islower() else s.append(i) for i in p]
>>> s
>>> ['this', 'that', 'there', 'is','apple']

此解决方案将创建一个单独的列表,其中包含小写项,而不管它们的原始大小写。如果原始的大小写是大写,那么列表s将包含列表p中相应项的小写。如果列表p中列表项的原始大小写已经是小写,那么列表s将保留该项的大小写并保持其小写。现在你可以用列表s代替列表p。


如果你的目的是通过一次转换来匹配另一个字符串,你也可以使用str.casefold()。

这是有用的,当你有非ascii字符和匹配ascii版本(例如:maße vs masse)。虽然str.lower或str.upper在这种情况下失败,但str.casefold()将通过。 这在Python 3中可用,详细讨论了这个想法,答案是https://stackoverflow.com/a/31599276/4848659。

>>>str="Hello World";
>>>print(str.lower());
hello world
>>>print(str.upper());
HELLO WOLRD
>>>print(str.casefold());
hello world

如果你试图将列表中的所有字符串转换为小写,你可以使用pandas:

import pandas as pd

data = ['Study', 'Insights']

pd_d = list(pd.Series(data).str.lower())

输出:

['study', 'insights']

上面的答案由@Amorpheuses给出了一个更简单的版本。

使用val中的值列表:

valsLower = [item.lower() for item in vals]

使用f = open()文本源,这对我来说工作得很好。


你可以尝试使用:

my_list = ['india', 'america', 'china', 'korea']

def capitalize_list(item):
    return item.upper()

print(list(map(capitalize_list, my_list)))

这里有另一个解决方案,但我不建议使用它。因为之前没有添加这个解决方案,所以把它放在这里完成这个主题。

import timeit

def foo1():
    L = ["A", "B", "C", "&"]
    return [x.lower() for x in L]
def foo2():
    L = ["A", "B", "C", "&"]
    return "%".join(L).lower().split("%")

for i in range(10):
    print("foo1", timeit.timeit(foo1, number=100000))
    print("foo2", timeit.timeit(foo2, number=100000), end="\n\n")
foo1 0.0814619
foo2 0.058695300000000006

foo1 0.08401910000000004
foo2 0.06001100000000004

foo1 0.08252670000000001
foo2 0.0601641

foo1 0.08721100000000004
foo2 0.06254229999999994

foo1 0.08776279999999992
foo2 0.05946070000000003

foo1 0.08383590000000007
foo2 0.05982449999999995

foo1 0.08354679999999992
foo2 0.05930219999999997

foo1 0.08526650000000013
foo2 0.060690699999999875

foo1 0.09940110000000013
foo2 0.08484609999999981

foo1 0.09921800000000003
foo2 0.06182889999999985