如何在列表理解Python中构建两个for循环


101

我有两个清单如下

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)

如何将两个循环写为单行列表理解?


3
itertools.chain如果您想要扁平化的列表,请使用:list(chain.from_iterable(entry for tag in tags for entry in entries if tag in entry))
Ashwini Chaudhary 2013年

Answers:


135

应该这样做:

[entry for tag in tags for entry in entries if tag in entry]

156

记住这一点的最好方法是,列表理解中for循环的顺序基于它们在传统循环方法中出现的顺序。最外面的循环先到,然后是内部循环。

因此,等效列表理解为:

[entry for tag in tags for entry in entries if tag in entry]

通常,if-else语句位于第一个for循环之前,如果只有一条if语句,它将位于结尾。例如,如果您想添加一个空列表,如果tag没有输入,则可以这样:

[entry if tag in entry else [] for tag in tags for entry in entries]

6

适当的LC将是

[entry for tag in tags for entry in entries if tag in entry]

LC中循环的顺序类似于嵌套循环中的顺序,if语句移至末尾,条件表达式移至开始,例如

[a if a else b for a in sequence]

观看演示-

>>> tags = [u'man', u'you', u'are', u'awesome']
>>> entries = [[u'man', u'thats'],[ u'right',u'awesome']]
>>> [entry for tag in tags for entry in entries if tag in entry]
[[u'man', u'thats'], [u'right', u'awesome']]
>>> result = []
    for tag in tags:
        for entry in entries:
            if tag in entry:
                result.append(entry)


>>> result
[[u'man', u'thats'], [u'right', u'awesome']]

编辑 -由于您需要将结果展平,因此可以使用类似的列表理解,然后展平结果。

>>> result = [entry for tag in tags for entry in entries if tag in entry]
>>> from itertools import chain
>>> list(chain.from_iterable(result))
[u'man', u'thats', u'right', u'awesome']

加起来,你可以做

>>> list(chain.from_iterable(entry for tag in tags for entry in entries if tag in entry))
[u'man', u'thats', u'right', u'awesome']

您在此处使用生成器表达式,而不是列表推导。(也完全匹配79个字符的限制(无list呼叫))


2
tags = [u'man', u'you', u'are', u'awesome']
entries = [[u'man', u'thats'],[ u'right',u'awesome']]

result = []
[result.extend(entry) for tag in tags for entry in entries if tag in entry]

print(result)

输出:

['man', 'thats', 'right', 'awesome']

0

理解上,嵌套列表迭代应遵循与forbriced for循环相同的顺序。

为了理解,我们将以NLP为例。您想从句子列表中创建所有单词的列表,其中每个句子都是单词列表。

>>> list_of_sentences = [['The','cat','chases', 'the', 'mouse','.'],['The','dog','barks','.']]
>>> all_words = [word for sentence in list_of_sentences for word in sentence]
>>> all_words
['The', 'cat', 'chases', 'the', 'mouse', '.', 'The', 'dog', 'barks', '.']

要删除重复的单词,可以使用集合{}代替列表[]

>>> all_unique_words = list({word for sentence in list_of_sentences for word in sentence}]
>>> all_unique_words
['.', 'dog', 'the', 'chase', 'barks', 'mouse', 'The', 'cat']

或申请 list(set(all_words))

>>> all_unique_words = list(set(all_words))
['.', 'dog', 'the', 'chases', 'barks', 'mouse', 'The', 'cat']

0
return=[entry for tag in tags for entry in entries if tag in entry for entry in entry]

6
嗨,欢迎来到Stack Overflow!请发布说明,而不只是代码。
伊芙琳

1
你好!尽管这段代码可以解决问题,但包括解释如何以及为何解决该问题的说明,确实可以帮助提高您的帖子质量,并可能导致更多的投票。请记住,您将来会为读者回答问题,而不仅仅是现在问的人。请编辑您的答案以添加说明,并指出适用的限制和假设。
布赖恩
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.