我正在使用Python进行一些集合运算,发现了一些奇怪的东西...
>> set([1,2,3]) | set([2,3,4])
set([1, 2, 3, 4])
>> set().union(*[[1,2,3], [2,3,4]])
set([1, 2, 3, 4])
这很好,符合预期的行为 - 但考虑交集的情况:
>> set([1,2,3]) & set([2,3,4])
set([2, 3])
>> set().intersection(*[[1,2,3], [2,3,4]])
set([])
我是不是疯了?为什么set.intersection()的操作结果和我的预期不一样?
如果我想像使用union那样对多个集合求交集(假设[[1,2,3], [2,3,4]]
有很多个列表),我该怎么做?用"Pythonic"的方式应该怎么做?
set.intersection
与set().intersection
不同。 - roippi