简体   繁体   English

Python设置Union和设置Intersection的操作方式有何不同?

[英]Python set Union and set Intersection operate differently?

I'm doing some set operations in Python, and I noticed something odd.. 我正在用Python做一些set操作,我注意到有些奇怪的东西..

>> 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)? 我怎么能像工作一样做多个集合的交集(假设[[1,2,3], [2,3,4]]有更多的列表)? What would the "pythonic" way be? “pythonic”的方式是什么?

When you do set() you are creating an empty set. 当你执行set()你正在创建一个空集。 When you do set().intersection(...) you are intersecting this empty set with other stuff. 当你set().intersection(...)你将这个空集与其他东西相交。 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(*...) . 但是,你不能直接用你的方式做到这一点,因为在你的例子中你实际上并没有任何集合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]
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 make them all into sets 否则你可以它们全部放进去

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM