如何在列表理解python中构建两个for循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18551458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to frame two for loops in list comprehension python
提问by Shiva Krishna Bavandla
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
:
我想提取物项从entries
当他们在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?
如何将两个循环编写为单行列表理解?
采纳答案by Sukrit Kalra
This should do it:
这应该这样做:
[entry for tag in tags for entry in entries if tag in entry]
回答by Sukrit Kalra
The appropriate LC would be
适当的 LC 将是
[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
LC 中循环的顺序与嵌套循环中的相似,if 语句到最后,条件表达式在开始,类似于
[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))
您在这里使用生成器表达式而不是列表推导式。(也完全符合 79 个字符的限制(无需list
调用))
回答by Rohit Jain
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.
记住这一点的最好方法是,列表推导式中 for 循环的顺序基于它们在传统循环方法中出现的顺序。最外循环先出现,然后是内循环。
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:
一般来说,if-else
语句在第一个 for 循环之前,如果你只有一个if
语句,它会在最后。例如,如果您想添加一个空列表,如果tag
不在条目中,您可以这样做:
[entry if tag in entry else [] for tag in tags for entry in entries]
回答by Raghav Gupta
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']
回答by Claude COULOMBE
In comprehension, the nested lists iteration should follow the same order than the equivalent imbricated for loops.
从理解上讲,嵌套列表迭代应该遵循与等价的叠瓦 for 循环相同的顺序。
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.
为了理解,我们将从 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', '.']
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))
或申请 list(set(all_words))
>>> all_unique_words = list(set(all_words))
['.', 'dog', 'the', 'chases', 'barks', 'mouse', 'The', 'cat']