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

时间:2009-03-13 13:42:45

标签: python list intersection

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

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

你能帮我个忙吗?

相关

20 个答案:

答案 0 :(得分:892)

您无需定义交集。它已经是集合中的第一类。

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

答案 1 :(得分:176)

如果您需要:

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

这是您的Python 2解决方案:

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

在Python 3 filter中返回一个可迭代而不是list,因此您需要使用filter打包list()次调用:

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

说明:

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

答案 2 :(得分:59)

对于那些只想找到两个列表的交集的人来说,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]
     

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

print intersect(b1, b2)

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

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

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

答案 3 :(得分:27)

功能方法:

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

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

答案 4 :(得分:26)

纯列表理解版

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

展平变体:

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

嵌套变体:

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

答案 5 :(得分:21)

&运算符取两个集合。

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

答案 6 :(得分:14)

采用两个列表交叉的pythonic方法是:

[x for x in list1 if x in list2]

答案 7 :(得分:8)

您应该使用此代码扁平化(取自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

在您对列表进行展平后,您将以通常的方式执行交叉:


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

答案 8 :(得分:8)

自定义intersect以来,基本列表理解就足够了:

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

感谢S. Lott的评论和TM。的相关评论:

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

答案 9 :(得分:5)

假设:

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

我发现以下代码运行良好,如果使用set操作可能更简洁:

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

它得到了:

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

如果需要订单:

> c3 = [sorted(list(set(f)&set(c1))) for f in c2] 
我们得到了:

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

顺便说一句,对于更多的python风格,这个也很好:

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

答案 10 :(得分: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]

答案 11 :(得分:3)

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

如果只是数字,请调查如何“展平”列表,然后使用set()方法。

答案 12 :(得分: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]

答案 13 :(得分: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]

答案 14 :(得分:0)

可以通过reduce轻松创建

列表。

您需要使用初始化器-reduce函数中的第三个参数。

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

以上代码适用于python2和python3,但是您需要将reduce模块导入为from functools import reduce。有关详细信息,请参见下面的链接。

答案 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)

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

对我来说,这是非常优雅和快捷的方式:)

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

以下是设置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]

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

答案 18 :(得分:0)

我们可以使用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

答案 19 :(得分:-1)

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

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

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)