Take the intersection of an arbitrary number of lists in python

前端 未结 7 1926
失恋的感觉
失恋的感觉 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: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]
    

提交回复
热议问题