如何在链表理解python中构造两个for循环

时间:2022-10-30 10:19:06

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?

如何将这两个循环写成一个单行列表?

4 个解决方案

#1


66  

This should do it:

这应该这样做:

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

#2


83  

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语句,那么它将出现在最后。为e。g,如果你想添加一个空列表,如果标签不在条目中,你会这样做:

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

#3


3  

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

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个字符限制(没有列表调用))

#4


1  

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']

#1


66  

This should do it:

这应该这样做:

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

#2


83  

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语句,那么它将出现在最后。为e。g,如果你想添加一个空列表,如果标签不在条目中,你会这样做:

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

#3


3  

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

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个字符限制(没有列表调用))

#4


1  

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']