【发布时间】:2013-09-04 06:40:44
【问题描述】:
我有两个列表如下
tags = [u'man', u'you', u'are', u'awesome']
entries = [[u'man', u'thats'],[ u'right',u'awesome']]
我想从entries 中提取条目,当它们在tags 中时:
result = []
for tag in tags:
for entry in entries:
if tag in entry:
result.extend(entry)
如何将两个循环编写为单行列表理解?
【问题讨论】:
-
如果您想要一个扁平列表,请使用
itertools.chain:list(chain.from_iterable(entry for tag in tags for entry in entries if tag in entry))
标签: python list for-loop list-comprehension