如何在不使用“ |”的情况下将两组连接在一起

问题:如何在不使用“ |”的情况下将两组连接在一起

假定ST被分配了集合。不使用join运算符|,如何找到两个集合的并集?例如,找到交叉点:

S = {1, 2, 3, 4}
T = {3, 4, 5, 6}
S_intersect_T = { i for i in S if i in T }

那么如何在不使用的情况下在一行中找到两个集合的并集|呢?

Assume that S and T are assigned sets. Without using the join operator |, how can I find the union of the two sets? This, for example, finds the intersection:

S = {1, 2, 3, 4}
T = {3, 4, 5, 6}
S_intersect_T = { i for i in S if i in T }

So how can I find the union of two sets in one line without using |?


回答 0

您可以对集合使用联合方法: set.union(other_set)

请注意,它会返回一个新集合,即不会对其自身进行修改。

You can use union method for sets: set.union(other_set)

Note that it returns a new set i.e it doesn’t modify itself.


回答 1

您可以使用or_别名:

>>> from operator import or_
>>> from functools import reduce # python3 required
>>> reduce(or_, [{1, 2, 3, 4}, {3, 4, 5, 6}])
set([1, 2, 3, 4, 5, 6])

You could use or_ alias:

>>> from operator import or_
>>> from functools import reduce # python3 required
>>> reduce(or_, [{1, 2, 3, 4}, {3, 4, 5, 6}])
set([1, 2, 3, 4, 5, 6])

回答 2

如果您可以修改原始集(在某些情况下可能需要这样做),可以使用set.update()

S.update(T)

返回值是None,但S将更新为原始S和的并集T

If you are fine with modifying the original set (which you may want to do in some cases), you can use set.update():

S.update(T)

The return value is None, but S will be updated to be the union of the original S and T.


回答 3

假设您也无法使用s.union(t),等于s | t,您可以尝试

>>> from itertools import chain
>>> set(chain(s,t))
set([1, 2, 3, 4, 5, 6])

或者,如果您想理解,

>>> {i for j in (s,t) for i in j}
set([1, 2, 3, 4, 5, 6])

Assuming you also can’t use s.union(t), which is equivalent to s | t, you could try

>>> from itertools import chain
>>> set(chain(s,t))
set([1, 2, 3, 4, 5, 6])

Or, if you want a comprehension,

>>> {i for j in (s,t) for i in j}
set([1, 2, 3, 4, 5, 6])

回答 4

如果加入表示您是工会,请尝试以下方法:

set(list(s) + list(t))

这有点骇人听闻,但我想不出更好的衬里来做。

If by join you mean union, try this:

set(list(s) + list(t))

It’s a bit of a hack, but I can’t think of a better one liner to do it.


回答 5

假设您有2个清单

 A = [1,2,3,4]
 B = [3,4,5,6]

所以你可以找到A联盟B如下

 union = set(A).union(set(B))

另外,如果要查找相交和非相交,请按照以下步骤进行操作

 intersection = set(A).intersection(set(B))
 non_intersection = union - intersection

Suppose you have 2 lists

 A = [1,2,3,4]
 B = [3,4,5,6]

so you can find A Union B as follow

 union = set(A).union(set(B))

also if you want to find intersection and non-intersection you do that as follow

 intersection = set(A).intersection(set(B))
 non_intersection = union - intersection

回答 6

您可以像这样将两个集合解压缩成一个集合:

>>> set_1 = {1, 2, 3, 4}
>>> set_2 = {3, 4, 5, 6}
>>> union = {*set_1, *set_2}
>>> union
{1, 2, 3, 4, 5, 6}

*解包集。拆包是将可迭代项(例如集合或列表)表示为它产生的每个项目的地方。这意味着上面的示例简化为{1, 2, 3, 4, 3, 4, 5, 6},然后简化为,{1, 2, 3, 4, 5, 6}因为该集合只能包含唯一项。

You can just unpack both sets into one like this:

>>> set_1 = {1, 2, 3, 4}
>>> set_2 = {3, 4, 5, 6}
>>> union = {*set_1, *set_2}
>>> union
{1, 2, 3, 4, 5, 6}

The * unpacks the set. Unpacking is where an iterable (e.g. a set or list) is represented as every item it yields. This means the above example simplifies to {1, 2, 3, 4, 3, 4, 5, 6} which then simplifies to {1, 2, 3, 4, 5, 6} because the set can only contain unique items.


回答 7

您可以做union或简单的列表理解

[A.add(_) for _ in B]

A将拥有B的所有元素

You can do union or simple list comprehension

[A.add(_) for _ in B]

A would have all the elements of B