python 中列表(列表的列表)中列表的交集

Intersection of the lists in a list (list of lists) in python

我有一个列表列表,如下所示:

list = [[1,2,3],[2,3,4],[3,4,5],[3,5,6]]

我想在python2.7中找到它们的交集,我是说

intersect_list = [3]

谢谢。

首先,不要使用 list 作为变量名 - 它隐藏了内置的 class.

接下来就这样了

>>> a = [[1,2,3],[2,3,4],[3,4,5],[3,5,6]]
>>> set.intersection(*map(set,a))
{3}

map(set,a) 只是将其转换为集合列表。然后你只需打开列表并找到交集。

如果你真的需要结果作为列表,只需用 list(...)

包装调用