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

将包含字符串的Python列表转换为小写或大写

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

user219126 asked 2019-03-26T09:06:15Z

9个解决方案

311 votes

它可以通过列表推导来完成。 这些基本上采用map的形式。例如,要创建一个新列表,其中所有项目都是较低的(或在第二个片段中加上大写),您将使用:

>>> [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']

YOU answered 2019-03-26T09:06:32Z

42 votes

除了更容易阅读(对于许多人),列表理解也赢得了速度竞赛:

$ 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

Ned Deily answered 2019-03-26T09:06:57Z

28 votes

>>> map(str.lower,["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 = [element.upper() for element in x] ; x

['ONE', 'TWO', 'THREE']

>>> x = [element.lower() 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' '[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

John La Rooy answered 2019-03-26T09:08:03Z

2 votes

mylist = ['Mixed Case One', 'Mixed Case Two', 'Mixed Three']

print map(lambda x: x.lower(), mylist)

print map(lambda x: x.upper(), mylist)

Chirael answered 2019-03-26T09:08:21Z

1 votes

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

fruits=['orange', 'grape', 'kiwi', 'apple', 'mango', 'fig', 'lemon']

newList = []

for fruit in fruits:

newList.append(fruit.upper())

print(newlist)

Cristina answered 2019-03-26T09:08:46Z

0 votes

解:

>>> 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']

此解决方案将创建一个包含小写项目的单独列表,无论其原始情况如何。 如果原始案例为高,那么list s将包含list p中相应项目的小写。如果列表项的原始案例在list p中已经是小写,则list s将保留项目的大小写并将其保持为小写。 现在你可以使用list s而不是list p。

Sunil answered 2019-03-26T09:09:16Z

0 votes

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

当你有非ascii字符并与ascii版本匹配时(例如:maßevsmasse),这很有用。虽然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

Gimhani answered 2019-03-26T09:09:50Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值