2024-04-20 10:55:09 发布
网友
假设我的列表有["Apple","ball","caT","dog"] 然后它应该给我结果'ball和'dog'。你知道吗
["Apple","ball","caT","dog"]
'ball
'dog'
如何使用re.findall()来实现这一点?你知道吗
re.findall()
你根本不需要re.findall()在这里。你知道吗
用途:
[s for s in inputlist if s.islower()]
如果字符串中的所有字母都是小写的,^{} method返回True。你知道吗
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']
如果你问起如何用大炮杀死蚊子,我想你技术上可以做到。。。你知道吗
c = re.compile(r'[a-z]') [x for x in li if len(re.findall(c,x)) == len(x)] Out[29]: ['ball', 'dog']
(不要为此使用regex)
re.findall不是你想要的。它被设计用来处理单个字符串,而不是它们的列表。你知道吗
re.findall
相反,您可以使用^{}和^{}:
>>> lst = ["Apple", "ball", "caT", "dog"] >>> # list(filter(str.islower, lst)) if you are on Python 3.x. >>> filter(str.islower, lst) ['ball', 'dog'] >>>
你根本不需要
re.findall()
在这里。你知道吗用途:
如果字符串中的所有字母都是小写的,^{} method 返回
True
。你知道吗演示:
使用
re.findall()
在较大的字符串中查找小写文本,而不是在列表中:如果你问起如何用大炮杀死蚊子,我想你技术上可以做到。。。你知道吗
(不要为此使用regex)
re.findall
不是你想要的。它被设计用来处理单个字符串,而不是它们的列表。你知道吗相反,您可以使用^{} 和^{} :
相关问题 更多 >
编程相关推荐