Take the intersection of an arbitrary number of lists in python

前端 未结 7 1821
失恋的感觉
失恋的感觉 2020-12-21 05:37

Suppose I have a list of lists of elements which are all the same (i\'ll use ints in this example)

[range(100)[::4], range(100)[::3], range(100)         


        
相关标签:
7条回答
  • 2020-12-21 06:18

    Convert them to sets and use the set.intersection method, reducing over the list of sets:

    xs = [range(100)[::4], range(100)[::3], range(100)[::2], range(100)[::1]]
    reduce(set.intersection, [set(x) for x in xs])
    

    reduce is a functional programming device that iterates through any iterable and applies the function provided to the first two elements, then to the result and the next, and then the result of that and the next, and so on.

    0 讨论(0)
  • 2020-12-21 06:27

    I think the built-in set module should do the trick.

    >>> elements = [range(100)[::4], range(100)[::3], range(100)[::2], range(100)[::1]]
    >>> sets = map(set, elements)
    >>> result = list(reduce(lambda x, y: x & y, sets))
    >>> print result
    [0, 96, 36, 72, 12, 48, 84, 24, 60]
    
    0 讨论(0)
  • 2020-12-21 06:29

    Use sets, which have an intersection method.

    >>> s = set()
    >>> s.add(4)
    >>> s.add(5)
    >>> s
    set([4, 5])
    >>> t = set([2, 4, 9])
    >>> s.intersection(t)
    set([4])
    

    For your example, something like

    >>> data = [range(100)[::4], range(100)[::3], range(100)[::2], range(100)[::1]]
    >>> sets = map(set, data)
    >>> print set.intersection(*sets)
    set([0, 96, 36, 72, 12, 48, 84, 24, 60])
    
    0 讨论(0)
  • 2020-12-21 06:31

    You can treat them as sets and use set.intersection():

    lists = [range(100)[::4], range(100)[::3], range(100)[::2], range(100)[::1]]
    sets = [set(l) for l in lists]
    
    isect = reduce(lambda x,y: x.intersection(y), sets)
    
    0 讨论(0)
  • 2020-12-21 06:33
    l = [range(100)[::4], range(100)[::3], range(100)[::2], range(100)[::1]]
    l = [set(i) for i in l]
    intersect = l[0].intersection(l[1])
    for i in l[2:]:
        intersect = intersect.intersection(i)
    
    0 讨论(0)
  • 2020-12-21 06:35

    I'm going to answer my own question:

    lists =  [range(100)[::4],range(100)[::3],range(100)[::2],range(100)[::1]]
    
    out = set(lists[0])
    for l in lists[1:]:
        out = set(l).intersection(out)
    
    print out
    

    or

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