Python set Union and set Intersection operate differently?

我的未来我决定 提交于 2019-12-20 08:30:08

问题


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,3,4]])
set([1, 2, 3, 4])

That's good, expected behaviour - but with intersection:

>> set([1,2,3]) & set([2,3,4])
set([2, 3])
>> set().intersection(*[[1,2,3], [2,3,4]])
set([])

Am I losing my mind here? Why isn't set.intersection() operating as I'd expect it to?

How can I do the intersection of many sets as I did with union (assuming the [[1,2,3], [2,3,4]] had a whole bunch more lists)? What would the "pythonic" way be?


回答1:


When you do set() you are creating an empty set. When you do set().intersection(...) you are intersecting this empty set with other stuff. The intersection of an empty set with any other collection of sets is empty.

If you actually have a list of sets, you can get their intersection similar to how you did it.

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

You can't do this directly with the way you're doing it, though, because you don't actually have any sets at all in your example with intersection(*...). You just have a list of lists. You should first convert the elements in your list to sets. So if you have

x = [[1,2,3], [2,3,4]]

you should do

x = [set(a) for a in x]



回答2:


set().intersection(*[[1,2,3], [2,3,4]])

is of course empty because you start with the empty set and intersect it with all the others

You can try calling the method on the class

set.intersection(*[[1,2,3], [2,3,4]])

but that won't work because the first argument passed needs to be a set

set.intersection({1, 2, 3}, *[[2,3,4], ...])

This looks awkward, better if you could use a list of sets in the first place. Especially if they are coming from a generator which makes it difficult to pull off the first item cleanly

set.intersection(*[{1,2,3}, {2,3,4}])

Otherwise you can just make them all into sets

set.intersection(*(set(x) for x in [[1,2,3], [2,3,4]]))



回答3:


convert the list to set first

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

For multiple lists you can just use,

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



回答4:


[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])


来源:https://stackoverflow.com/questions/19580944/python-set-union-and-set-intersection-operate-differently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!