Take the intersection of an arbitrary number of lists in python

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

    Here's a one-liner using the good old all() built-in function:

    list(num for num in data[0] 
         if all(num in range_ for range_ in data[1:]))
    

    Interestingly, this is (I think) more readable and faster than using set for larger data sets.

提交回复
热议问题