Take the intersection of an arbitrary number of lists in python

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

提交回复
热议问题