我有:
data = [[u'text'],[u'element'],[u'text00']]
pattern = text
pattern2 = EL
pattern3 = 00
使用正则表达式我想搜索并返回:
text, text00 # for pattern
element # for pattern2
text00 # for pattern3
import re
data = [[u'text'], [u'element'], [u'text00']]
patterns = [u'text', u'EL', u'00']
results = []
for pattern in patterns:
results.append([x[0] for x in data if re.search(pattern, x[0], flags=re.I)])
print results
或者,更简洁地说:
import re
data = [[u'text'], [u'element'], [u'text00']]
patterns = [u'text', u'EL', u'00']
results = [[x[0] for x in data if re.search(pattern, x[0], flags=re.I)] for pattern in patterns]
print results
我认为您正在寻找的是any()
:
>>> L = ["red", "lightred", "orange red", "blue"]
>>> keyword = 'red'
>>> import re
>>> any(re.search(keyword, i) is not None for i in L)
True