python set_Python Set联合

python set

In set theory, the union of a collection of sets is the set of all the elements in the collection of the sets. Following image depicts the set union operations between a collection of sets.

在集合论中,集合集合的并集是集合集合中所有元素的集合。 下图描述了集合集合之间的集合联合操作。

Set Union

Set Union

设置联盟

Python Set联合 (Python Set Union)

Python set class provides union() function to get the union of a collection of sets. The result is a new set with all the elements from the collection of sets.

Python set类提供union()函数来获取集合集合的并集。 结果是一个新集合,其中包含集合集合中的所有元素。

Let’s look at some examples of Python set union() function.

让我们看一些Python设置union()函数的示例。

set1 = {1, 2, 3, 4}
set2 = {2, 3, 5, 6}
set3 = {3, 4, 6, 7}

print(set1.union(set2))
print(set2.union(set3))
print(set3.union(set1))

Output:

输出:

{1, 2, 3, 4, 5, 6}
{2, 3, 4, 5, 6, 7}
{1, 2, 3, 4, 6, 7}
Python Set Union

Python Set Union

Python Set联合

多集的并集 (Union of Multiple Sets)

We can create the union of multiple sets through two ways.

我们可以通过两种方式创建多个集合的并集。

  1. By passing multiple sets as argument in union() function.

    通过在union()函数中将多个集合作为参数传递。
  2. Since union() returns a new set, we can create a chain of union() function calls.

    由于union()返回一个新集合,因此我们可以创建一个union()函数调用链。

Below code snippet shows above two ways implementation.

下面的代码片段显示了以上两种方式的实现。

print(set1.union(set2, set3))
#  OR
print(set1.union(set2).union(set3))

Output:

输出:

{1, 2, 3, 4, 5, 6, 7}
{1, 2, 3, 4, 5, 6, 7}
Python Multiple Sets Union

Python Multiple Sets Union

Python多集联合

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/24769/python-set-union

python set

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值