Python set Union and set Intersection operate differently?

后端 未结 4 1485
无人及你
无人及你 2021-01-30 16:25

I\'m doing some set operations in Python, and I noticed something odd..

>> set([1,2,3]) | set([2,3,4])
set([1, 2, 3, 4])
>> set().union(*[[1,2,3], [2         


        
4条回答
  •  北海茫月
    2021-01-30 17:16

    [removed incorrect answer]

    As @Anto Vinish suggested you should first convert the lists to sets and then use set.intersection

    For example:

    >>> sets = [set([1, 2, 3]), set([2, 3, 4]), set([3, 4, 5])]
    >>> set.intersection(*sets)
    set([3])
    

提交回复
热议问题