Python set Union 和 set Intersection 操作不同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19580944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:06:18  来源:igfitidea点击:

Python set Union and set Intersection operate differently?

pythonsetunionintersection

提问by Bilal Akil

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

我正在用 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])

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?

我在这里失去理智了吗?为什么 set.intersection() 没有像我期望的那样运行?

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?

我怎样才能像使用 union 那样做许多集合的交集(假设[[1,2,3], [2,3,4]]有一大堆更多的列表)?“pythonic”方式是什么?

采纳答案by BrenBarn

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.

当你这样做时,set()你正在创建一个空集。当你这样做时,set().intersection(...)你是在把这个空集与其他东西相交。空集与任何其他集集合的交集是空的。

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

但是,您不能直接按照您的方式执行此操作,因为在您的示例中实际上根本没有任何集合intersection(*...). 您只有一个列表列表。您应该首先将列表中的元素转换为集合。所以如果你有

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

you should do

你应该做

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

回答by Hypuk

[removed incorrect answer]

[删除错误答案]

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

正如@Anto Vinish 建议您首先将列表转换为集合,然后使用 set.intersection

For example:

例如:

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

回答by Anto

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

回答by John La Rooy

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

您可以尝试在 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 makethem all into sets

否则,您可以它们全部组合成套

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