2009-11-12 56 views

回答

8
common = set(x[0]) 
for l in x[1:]: 
    common &= set(l) 
print list(common) 

或:

import operator 
print reduce(operator.iand, map(set, x)) 
+0

我最初是用set()开始的,现在已经修复了。 – 2009-11-12 15:38:26

+1

是的,我发布了一个修改你的工作的答案,但到了我回来的时候你就编辑了它。可悲的是,stackoverflow不会让我+1你,因为我upvoted,然后解开我upvote。虽然很好的答案! – 2009-11-12 15:40:01

6

在一个班轮:

>>> reduce(set.intersection, x[1:], set(x[0])) 
set([3, 4]) 
+0

或者列表(reduce(set.intersection,x [1:],set(x [0]))) – grokus 2009-11-12 16:12:59

1
def f(a, b): 
    return list(set(a).intersection(set(b))) 

reduce(f, x) 
0

解决,如纳迪亚但不使用降低几乎相同的另一种方式,我用地图:

>>> x = [ [1, 2, 3, 4], [3, 4, 6, 7], [2, 3, 4, 6, 7]] 
>>> set.intersection(*map(set,x)) 
set([3, 4]) 
>>>