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

477
我知道如何获取两个平面列表的交集:
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]
或者
def intersect(a, b):
    return list(set(a) & set(b))
 
print intersect(b1, b2)

但是当我需要为嵌套列表查找交集时,我的问题就开始了:

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

你们能帮我一下吗?

相关


你对于 c1 和 c2 的交集会是什么?你只是想找出 c1 是否在 c2 中吗?还是你想找出 c1 中出现在 c2 中的所有元素? - Brian R. Bondy
阅读这个并在解释器中操作。 - Pithikos
21个回答

3

我不知道是否回答你的问题已经有些晚了。在阅读了你的问题之后,我想出了一个可以同时处理列表和嵌套列表的函数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

例子:

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

3
你认为[1,2][1, [2]]相交吗?也就是说,你只关心数字还是列表结构?如果只关心数字,请研究如何“展开”这些列表,然后使用set()方法。

我希望保持列表结构不变。 - elfuego1

1

我也在寻找一种方法来做到这一点,最终它变成了这样:

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]

如果不使用set.intersection,那么我也会使用这些简单的一行代码。 - slaughter98

1

要正确考虑元素的基数定义交集,请使用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]

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

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

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

   print result

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

对我来说,这是一种非常优雅和快速的方法 :)


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

以下是一种不涉及设置的设置c3的方法:

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

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

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

这是一个列表推导式内嵌在另一个列表推导式中,有点不寻常,但我认为你不应该遇到太多麻烦。


0

在可迭代对象之间查找差异和交集的简单方法

如果重复很重要,请使用此方法

from collections import Counter

def intersection(a, b):
    """
    Find the intersection of two iterables

    >>> intersection((1,2,3), (2,3,4))
    (2, 3)

    >>> intersection((1,2,3,3), (2,3,3,4))
    (2, 3, 3)

    >>> intersection((1,2,3,3), (2,3,4,4))
    (2, 3)

    >>> intersection((1,2,3,3), (2,3,4,4))
    (2, 3)
    """
    return tuple(n for n, count in (Counter(a) & Counter(b)).items() for _ in range(count))

def difference(a, b):
    """
    Find the symmetric difference of two iterables

    >>> difference((1,2,3), (2,3,4))
    (1, 4)

    >>> difference((1,2,3,3), (2,3,4))
    (1, 3, 4)

    >>> difference((1,2,3,3), (2,3,4,4))
    (1, 3, 4, 4)
    """
    diff = lambda x, y: tuple(n for n, count in (Counter(x) - Counter(y)).items() for _ in range(count))
    return diff(a, b) + diff(b, a)

0

通过reduce很容易制作平面列表。

你只需要使用initializer - reduce函数的第三个参数。

reduce(
   lambda result, _list: result.append(
       list(set(_list)&set(c1)) 
     ) or result, 
   c2, 
   [])

以上代码适用于Python2和Python3,但您需要导入reduce模块,如from functools import reduce。请参考下面的链接获取详细信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接