Find intersection of two nested lists?

前端 未结 20 1114
星月不相逢
星月不相逢 2020-11-22 04:16

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

<
相关标签:
20条回答
  • 2020-11-22 04:47

    Simple way to find difference and intersection between iterables

    Use this method if repetition matters

    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 讨论(0)
  • 2020-11-22 04:48

    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:

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

    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.

    0 讨论(0)
  • 2020-11-22 04:50

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

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

    >>> c3 = [list(set(c1).intersection(i)) for i in c2]
    >>> c3
    [[32, 13], [28, 13, 7], [1, 6]]
    
    0 讨论(0)
  • 2020-11-22 04:52
    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 讨论(0)
  • 2020-11-22 04:53

    We can use set methods for this:

    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 讨论(0)
  • 2020-11-22 04:54
    # 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 = []
    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.

    0 讨论(0)
提交回复
热议问题