假设我的列表中有["Apple","ball","caT","dog"]
那么它应该给我结果'ball
和'dog'
。
如何使用re.findall()
?
答案 0 :(得分:5)
您根本不需要re.findall()
。
使用:
[s for s in inputlist if s.islower()]
如果字符串中的所有字母都是低位的,str.islower()
method会返回True
。
演示:
>>> inputlist = ["Apple","ball","caT","dog"]
>>> [s for s in inputlist if s.islower()]
['ball', 'dog']
使用re.findall()
在较大的字符串中查找小写文字,而不是在列表中:
>>> import re
>>> re.findall(r'\b[a-z]+\b', 'The quick Brown Fox jumped!')
['quick', 'jumped']
答案 1 :(得分:3)
re.findall
不是你想要的。它被设计为使用单个字符串,而不是它们的列表。
相反,您可以使用filter
和str.islower
:
>>> lst = ["Apple", "ball", "caT", "dog"]
>>> # list(filter(str.islower, lst)) if you are on Python 3.x.
>>> filter(str.islower, lst)
['ball', 'dog']
>>>
答案 2 :(得分:0)
询问如何用大炮杀死蚊子,我想你可以从技术上做到这一点......
c = re.compile(r'[a-z]')
[x for x in li if len(re.findall(c,x)) == len(x)]
Out[29]: ['ball', 'dog']
(不要使用正则表达式)