找到两个嵌套列表的交集?

时间:2022-05-30 11:30:07

I know how to get an intersection of two flat lists:

我知道如何得到两个平面列表的交集:

b1 = [1,2,3,4,5,9,11,15]
b2 = [4,5,6,7,8]
b3 = [val for val in b1 if val in b2]

or

def intersect(a, b):
    return list(set(a) & set(b))

print intersect(b1, b2)

But when I have to find intersection for nested lists then my problems starts:

但是当我必须找到嵌套列表的交集时,我的问题就开始了:

c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]

In the end I would like to receive:

最后我想收到:

c3 = [[13,32],[7,13,28],[1,6]]

Can you guys give me a hand with this?

你们能帮我一下吗?

Related

18 个解决方案

#1


163  

If you want:

如果你想要:

c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]
c3 = [[13, 32], [7, 13, 28], [1,6]]

Then here is your solution for Python 2:

那么这就是Python 2的解决方案:

c3 = [filter(lambda x: x in c1, sublist) for sublist in c2]

In Python 3 filter returns an iterable instead of list, so you need to wrap filter calls with list():

在Python 3中,filter返回一个可迭代的而不是list,所以您需要使用list()包装过滤器调用:

c3 = [list(filter(lambda x: x in c1, sublist)) for sublist in c2]

Explanation:

解释:

The filter part takes each sublist's item and checks to see if it is in the source list c1. The list comprehension is executed for each sublist in c2.

过滤器部分获取每个子列表的项并检查它是否在源列表c1中。对c2中的每个子列表执行列表理解。

#2


855  

You don't need to define intersection. It's already a first-class part of set.

你不需要定义交集。这已经是片场的头等大事了。

>>> b1 = [1,2,3,4,5,9,11,15]
>>> b2 = [4,5,6,7,8]
>>> set(b1).intersection(b2)
set([4, 5])

#3


55  

For people just looking to find the intersection of two lists, the Asker provided two methods:

对于那些只想找到两个列表交集的人来说,Asker提供了两种方法:

b1 = [1,2,3,4,5,9,11,15]
b2 = [4,5,6,7,8]
b3 = [val for val in b1 if val in b2]

and

def intersect(a, b):
     return list(set(a) & set(b))

print intersect(b1, b2)

But there is a hybrid method that is more efficient, because you only have to do one conversion between list/set, as opposed to three:

但是有一种混合方法更有效,因为你只需要在列表/集合之间进行一次转换,而不是三个:

b1 = [1,2,3,4,5]
b2 = [3,4,5,6]
s2 = set(b2)
b3 = [val for val in b1 if val in s2]

This will run in O(n), whereas his original method involving list comprehension will run in O(n^2)

这将运行在O(n),而他的原始方法涉及列表理解将运行在O(n ^ 2)

#4


25  

Pure list comprehension version

>>> c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
>>> c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]
>>> c1set = frozenset(c1)

Flatten variant:

平变体:

>>> [n for lst in c2 for n in lst if n in c1set]
[13, 32, 7, 13, 28, 1, 6]

Nested variant:

嵌套的变体:

>>> [[n for n in lst if n in c1set] for lst in c2]
[[13, 32], [7, 13, 28], [1, 6]]

#5


25  

The functional approach:

功能的方法:

input_list = [[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]]

result = reduce(set.intersection, map(set, input_list))

and it can be applied to the more general case of 1+ lists

它可以应用于1+ list的更一般的情况

#6


19  

The & operator takes the intersection of two sets.

&算子取两个集合的交点。

{1, 2, 3} & {2, 3, 4} Out[1]: {2, 3}

{1,2,3} & {2,3,4} Out[1]: {2,3}

#7


11  

A pythonic way of taking intersection of 2 lists is:

用毕达哥拉斯的方法求两个列表的交集是:

    [x for x in list1 if x in list2]

#8


7  

You should flatten using this code ( taken from http://kogs-www.informatik.uni-hamburg.de/~meine/python_tricks ), the code is untested, but I'm pretty sure it works:

您应该使用这段代码(取自http://kogs-www.informatik.uni-hamburg.de/~meine/python_tricks)来简化代码,该代码没有经过测试,但我确信它是有效的:


def flatten(x):
    """flatten(sequence) -> list

    Returns a single, flat list which contains all elements retrieved
    from the sequence and all recursively contained sub-sequences
    (iterables).

    Examples:
    >>> [1, 2, [3,4], (5,6)]
    [1, 2, [3, 4], (5, 6)]
    >>> flatten([[[1,2,3], (42,None)], [4,5], [6], 7, MyVector(8,9,10)])
    [1, 2, 3, 42, None, 4, 5, 6, 7, 8, 9, 10]"""

    result = []
    for el in x:
        #if isinstance(el, (list, tuple)):
        if hasattr(el, "__iter__") and not isinstance(el, basestring):
            result.extend(flatten(el))
        else:
            result.append(el)
    return result

After you had flattened the list, you perform the intersection in the usual way:

在你把列表变平之后,你按照通常的方式进行交叉:


c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]

def intersect(a, b):
     return list(set(a) & set(b))

print intersect(flatten(c1), flatten(c2))

#9


6  

Since intersect was defined, a basic list comprehension is enough:

既然定义了intersect,那么基本的列表理解就足够了:

>>> c3 = [intersect(c1, i) for i in c2]
>>> c3
[[32, 13], [28, 13, 7], [1, 6]]

Improvement thanks to S. Lott's remark and TM.'s associated remark:

多亏了s。Lott的评论和TM。相关的备注:

>>> c3 = [list(set(c1).intersection(i)) for i in c2]
>>> c3
[[32, 13], [28, 13, 7], [1, 6]]

#10


5  

Given:

考虑到:

> c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]

> c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]

I find the following code works well and maybe more concise if using set operation:

我发现下面的代码运行得很好,如果使用set操作可能更简洁:

> c3 = [list(set(f)&set(c1)) for f in c2] 

It got:

它有:

> [[32, 13], [28, 13, 7], [1, 6]]

If order needed:

如果订单需要:

> c3 = [sorted(list(set(f)&set(c1))) for f in c2] 

we got:

我们有:

> [[13, 32], [7, 13, 28], [1, 6]]

By the way, for a more python style, this one is fine too:

顺便说一下,对于python风格,这个也可以:

> c3 = [ [i for i in set(f) if i in c1] for f in c2]

#11


3  

Do you consider [1,2] to intersect with [1, [2]]? That is, is it only the numbers you care about, or the list structure as well?

你认为[1,2]与[1,[2]]相交吗?也就是说,它只是你关心的数字,还是列表结构?

If only the numbers, investigate how to "flatten" the lists, then use the set() method.

如果只有数字,研究如何“使”列表“变平”,然后使用set()方法。

#12


3  

I don't know if I am late in answering your question. After reading your question I came up with a function intersect() that can work on both list and nested list. I used recursion to define this function, it is very intuitive. Hope it is what you are looking for:

我不知道我回答你的问题是否迟到。在阅读了您的问题之后,我提出了一个函数intersect(),可以同时处理列表和嵌套列表。我用递归定义这个函数,很直观。希望这就是你想要的:

def intersect(a, b):
    result=[]
    for i in b:
        if isinstance(i,list):
            result.append(intersect(a,i))
        else:
            if i in a:
                 result.append(i)
    return result

Example:

例子:

>>> c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
>>> c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]
>>> print intersect(c1,c2)
[[13, 32], [7, 13, 28], [1, 6]]

>>> b1 = [1,2,3,4,5,9,11,15]
>>> b2 = [4,5,6,7,8]
>>> print intersect(b1,b2)
[4, 5]

#13


1  

I was also looking for a way to do it, and eventually it ended up like this:

我也在寻找一种方法来做这件事,最终结果是这样的:

def compareLists(a,b):
    removed = [x for x in a if x not in b]
    added = [x for x in b if x not in a]
    overlap = [x for x in a if x in b]
    return [removed,added,overlap]

#14


1  

To define intersection that correctly takes into account the cardinality of the elements use Counter:

要正确定义考虑元素基数的交集,使用计数器:

from collections import Counter

>>> c1 = [1, 2, 2, 3, 4, 4, 4]
>>> c2 = [1, 2, 4, 4, 4, 4, 5]
>>> list((Counter(c1) & Counter(c2)).elements())
[1, 2, 4, 4, 4]

#15


0  

c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]

c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]

c3 = [list(set(c2[i]).intersection(set(c1))) for i in xrange(len(c2))]

c3
->[[32, 13], [28, 13, 7], [1, 6]]

#16


0  

We can use set methods for this:

我们可以为此使用set方法:

c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]

   result = [] 
   for li in c2:
       res = set(li) & set(c1)
       result.append(list(res))

   print result

#17


0  

# Problem:  Given c1 and c2:
c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]
# how do you get c3 to be [[13, 32], [7, 13, 28], [1, 6]] ?

Here's one way to set c3 that doesn't involve sets:

这里有一种方法可以使c3不包含集合:

c3 = []
for sublist in c2:
    c3.append([val for val in c1 if val in sublist])

But if you prefer to use just one line, you can do this:

但是如果你喜欢只用一行,你可以这样做:

c3 = [[val for val in c1 if val in sublist]  for sublist in c2]

It's a list comprehension inside a list comprehension, which is a little unusual, but I think you shouldn't have too much trouble following it.

这是一个列表理解中的列表理解,这有点不寻常,但我认为你不应该有太多的麻烦遵循它。

#18


0  

c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]
c3 = [list(set(i) & set(c1)) for i in c2]
c3
[[32, 13], [28, 13, 7], [1, 6]]

For me this is very elegant and quick way to to it :)

对我来说,这是一种非常优雅、快捷的方式:

#1


163  

If you want:

如果你想要:

c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]
c3 = [[13, 32], [7, 13, 28], [1,6]]

Then here is your solution for Python 2:

那么这就是Python 2的解决方案:

c3 = [filter(lambda x: x in c1, sublist) for sublist in c2]

In Python 3 filter returns an iterable instead of list, so you need to wrap filter calls with list():

在Python 3中,filter返回一个可迭代的而不是list,所以您需要使用list()包装过滤器调用:

c3 = [list(filter(lambda x: x in c1, sublist)) for sublist in c2]

Explanation:

解释:

The filter part takes each sublist's item and checks to see if it is in the source list c1. The list comprehension is executed for each sublist in c2.

过滤器部分获取每个子列表的项并检查它是否在源列表c1中。对c2中的每个子列表执行列表理解。

#2


855  

You don't need to define intersection. It's already a first-class part of set.

你不需要定义交集。这已经是片场的头等大事了。

>>> b1 = [1,2,3,4,5,9,11,15]
>>> b2 = [4,5,6,7,8]
>>> set(b1).intersection(b2)
set([4, 5])

#3


55  

For people just looking to find the intersection of two lists, the Asker provided two methods:

对于那些只想找到两个列表交集的人来说,Asker提供了两种方法:

b1 = [1,2,3,4,5,9,11,15]
b2 = [4,5,6,7,8]
b3 = [val for val in b1 if val in b2]

and

def intersect(a, b):
     return list(set(a) & set(b))

print intersect(b1, b2)

But there is a hybrid method that is more efficient, because you only have to do one conversion between list/set, as opposed to three:

但是有一种混合方法更有效,因为你只需要在列表/集合之间进行一次转换,而不是三个:

b1 = [1,2,3,4,5]
b2 = [3,4,5,6]
s2 = set(b2)
b3 = [val for val in b1 if val in s2]

This will run in O(n), whereas his original method involving list comprehension will run in O(n^2)

这将运行在O(n),而他的原始方法涉及列表理解将运行在O(n ^ 2)

#4


25  

Pure list comprehension version

>>> c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
>>> c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]
>>> c1set = frozenset(c1)

Flatten variant:

平变体:

>>> [n for lst in c2 for n in lst if n in c1set]
[13, 32, 7, 13, 28, 1, 6]

Nested variant:

嵌套的变体:

>>> [[n for n in lst if n in c1set] for lst in c2]
[[13, 32], [7, 13, 28], [1, 6]]

#5


25  

The functional approach:

功能的方法:

input_list = [[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]]

result = reduce(set.intersection, map(set, input_list))

and it can be applied to the more general case of 1+ lists

它可以应用于1+ list的更一般的情况

#6


19  

The & operator takes the intersection of two sets.

&算子取两个集合的交点。

{1, 2, 3} & {2, 3, 4} Out[1]: {2, 3}

{1,2,3} & {2,3,4} Out[1]: {2,3}

#7


11  

A pythonic way of taking intersection of 2 lists is:

用毕达哥拉斯的方法求两个列表的交集是:

    [x for x in list1 if x in list2]

#8


7  

You should flatten using this code ( taken from http://kogs-www.informatik.uni-hamburg.de/~meine/python_tricks ), the code is untested, but I'm pretty sure it works:

您应该使用这段代码(取自http://kogs-www.informatik.uni-hamburg.de/~meine/python_tricks)来简化代码,该代码没有经过测试,但我确信它是有效的:


def flatten(x):
    """flatten(sequence) -> list

    Returns a single, flat list which contains all elements retrieved
    from the sequence and all recursively contained sub-sequences
    (iterables).

    Examples:
    >>> [1, 2, [3,4], (5,6)]
    [1, 2, [3, 4], (5, 6)]
    >>> flatten([[[1,2,3], (42,None)], [4,5], [6], 7, MyVector(8,9,10)])
    [1, 2, 3, 42, None, 4, 5, 6, 7, 8, 9, 10]"""

    result = []
    for el in x:
        #if isinstance(el, (list, tuple)):
        if hasattr(el, "__iter__") and not isinstance(el, basestring):
            result.extend(flatten(el))
        else:
            result.append(el)
    return result

After you had flattened the list, you perform the intersection in the usual way:

在你把列表变平之后,你按照通常的方式进行交叉:


c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]

def intersect(a, b):
     return list(set(a) & set(b))

print intersect(flatten(c1), flatten(c2))

#9


6  

Since intersect was defined, a basic list comprehension is enough:

既然定义了intersect,那么基本的列表理解就足够了:

>>> c3 = [intersect(c1, i) for i in c2]
>>> c3
[[32, 13], [28, 13, 7], [1, 6]]

Improvement thanks to S. Lott's remark and TM.'s associated remark:

多亏了s。Lott的评论和TM。相关的备注:

>>> c3 = [list(set(c1).intersection(i)) for i in c2]
>>> c3
[[32, 13], [28, 13, 7], [1, 6]]

#10


5  

Given:

考虑到:

> c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]

> c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]

I find the following code works well and maybe more concise if using set operation:

我发现下面的代码运行得很好,如果使用set操作可能更简洁:

> c3 = [list(set(f)&set(c1)) for f in c2] 

It got:

它有:

> [[32, 13], [28, 13, 7], [1, 6]]

If order needed:

如果订单需要:

> c3 = [sorted(list(set(f)&set(c1))) for f in c2] 

we got:

我们有:

> [[13, 32], [7, 13, 28], [1, 6]]

By the way, for a more python style, this one is fine too:

顺便说一下,对于python风格,这个也可以:

> c3 = [ [i for i in set(f) if i in c1] for f in c2]

#11


3  

Do you consider [1,2] to intersect with [1, [2]]? That is, is it only the numbers you care about, or the list structure as well?

你认为[1,2]与[1,[2]]相交吗?也就是说,它只是你关心的数字,还是列表结构?

If only the numbers, investigate how to "flatten" the lists, then use the set() method.

如果只有数字,研究如何“使”列表“变平”,然后使用set()方法。

#12


3  

I don't know if I am late in answering your question. After reading your question I came up with a function intersect() that can work on both list and nested list. I used recursion to define this function, it is very intuitive. Hope it is what you are looking for:

我不知道我回答你的问题是否迟到。在阅读了您的问题之后,我提出了一个函数intersect(),可以同时处理列表和嵌套列表。我用递归定义这个函数,很直观。希望这就是你想要的:

def intersect(a, b):
    result=[]
    for i in b:
        if isinstance(i,list):
            result.append(intersect(a,i))
        else:
            if i in a:
                 result.append(i)
    return result

Example:

例子:

>>> c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
>>> c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]
>>> print intersect(c1,c2)
[[13, 32], [7, 13, 28], [1, 6]]

>>> b1 = [1,2,3,4,5,9,11,15]
>>> b2 = [4,5,6,7,8]
>>> print intersect(b1,b2)
[4, 5]

#13


1  

I was also looking for a way to do it, and eventually it ended up like this:

我也在寻找一种方法来做这件事,最终结果是这样的:

def compareLists(a,b):
    removed = [x for x in a if x not in b]
    added = [x for x in b if x not in a]
    overlap = [x for x in a if x in b]
    return [removed,added,overlap]

#14


1  

To define intersection that correctly takes into account the cardinality of the elements use Counter:

要正确定义考虑元素基数的交集,使用计数器:

from collections import Counter

>>> c1 = [1, 2, 2, 3, 4, 4, 4]
>>> c2 = [1, 2, 4, 4, 4, 4, 5]
>>> list((Counter(c1) & Counter(c2)).elements())
[1, 2, 4, 4, 4]

#15


0  

c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]

c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]

c3 = [list(set(c2[i]).intersection(set(c1))) for i in xrange(len(c2))]

c3
->[[32, 13], [28, 13, 7], [1, 6]]

#16


0  

We can use set methods for this:

我们可以为此使用set方法:

c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]

   result = [] 
   for li in c2:
       res = set(li) & set(c1)
       result.append(list(res))

   print result

#17


0  

# Problem:  Given c1 and c2:
c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]
# how do you get c3 to be [[13, 32], [7, 13, 28], [1, 6]] ?

Here's one way to set c3 that doesn't involve sets:

这里有一种方法可以使c3不包含集合:

c3 = []
for sublist in c2:
    c3.append([val for val in c1 if val in sublist])

But if you prefer to use just one line, you can do this:

但是如果你喜欢只用一行,你可以这样做:

c3 = [[val for val in c1 if val in sublist]  for sublist in c2]

It's a list comprehension inside a list comprehension, which is a little unusual, but I think you shouldn't have too much trouble following it.

这是一个列表理解中的列表理解,这有点不寻常,但我认为你不应该有太多的麻烦遵循它。

#18


0  

c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]
c3 = [list(set(i) & set(c1)) for i in c2]
c3
[[32, 13], [28, 13, 7], [1, 6]]

For me this is very elegant and quick way to to it :)

对我来说,这是一种非常优雅、快捷的方式: