问题:如何在列表理解Python中构建两个for循环
我有两个清单如下
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)
如何将两个循环写为单行列表理解?
I have two lists as below
tags = [u'man', u'you', u'are', u'awesome']
entries = [[u'man', u'thats'],[ u'right',u'awesome']]
I want to extract entries from entries
when they are in tags
:
result = []
for tag in tags:
for entry in entries:
if tag in entry:
result.extend(entry)
How can I write the two loops as a single line list comprehension?
回答 0
应该这样做:
[entry for tag in tags for entry in entries if tag in entry]
This should do it:
[entry for tag in tags for entry in entries if tag in entry]
回答 1
记住这一点的最好方法是,列表理解中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]
The best way to remember this is that the order of for loop inside the list comprehension is based on the order in which they appear in traditional loop approach. Outer most loop comes first, and then the inner loops subsequently.
So, the equivalent list comprehension would be:
[entry for tag in tags for entry in entries if tag in entry]
In general, if-else
statement comes before the first for loop, and if you have just an if
statement, it will come at the end. For e.g, if you would like to add an empty list, if tag
is not in entry, you would do it like this:
[entry if tag in entry else [] for tag in tags for entry in entries]
回答 2
适当的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
呼叫))
The appropriate LC would be
[entry for tag in tags for entry in entries if tag in entry]
The order of the loops in the LC is similar to the ones in nested loops, the if statements go to the end and the conditional expressions go in the beginning, something like
[a if a else b for a in sequence]
See the Demo –
>>> 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']]
EDIT – Since, you need the result to be flattened, you could use a similar list comprehension and then flatten the results.
>>> 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']
Adding this together, you could just do
>>> 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']
You use a generator expression here instead of a list comprehension. (Perfectly matches the 79 character limit too (without the list
call))
回答 3
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']
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)
Output:
['man', 'thats', 'right', 'awesome']
回答 4
理解上,嵌套列表迭代应遵循与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']
In comprehension, the nested lists iteration should follow the same order than the equivalent imbricated for loops.
To understand, we will take a simple example from NLP. You want to create a list of all words from a list of sentences where each sentence is a list of words.
>>> 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', '.']
To remove the repeated words, you can use a set {} instead of a list []
>>> 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']
or apply list(set(all_words))
>>> all_unique_words = list(set(all_words))
['.', 'dog', 'the', 'chases', 'barks', 'mouse', 'The', 'cat']
回答 5
return=[entry for tag in tags for entry in entries if tag in entry for entry in entry]
return=[entry for tag in tags for entry in entries if tag in entry for entry in entry]