忽然笑
从2.6开始,set.intersection任意多次迭代。>>> s1 = set([1, 2, 3])>>> s2 = set([2, 3, 4])>>> s3 = set([2, 4, 6])>>> s1 & s2 & s3set([2])>>> s1.intersection(s2, s3)set([2])>>> sets = [s1, s2, s3]>>> set.intersection(*sets)set([2])
慕婉清6462132
显然set.intersection你想要的是这里,但是如果你需要概括“把所有这些的总和”,“取所有这些的产品”,“把所有这些的xor”,你正在寻找的是reduce功能:from operator import and_from functools import reduceprint(reduce(and_, [{1,2,3},{2,3,4},{3,4,5}])) # = {3}要么print(reduce((lambda x,y: x&y), [{1,2,3},{2,3,4},{3,4,5}])) # = {3}