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](https://i-blog.csdnimg.cn/blog_migrate/6f555313b94eb72b85605d8ecfb01615.png)
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](https://cdn.journaldev.com/wp-content/uploads/2019/01/python-set-union.png)
Python Set Union
Python Set联合
多集的并集 (Union of Multiple Sets)
We can create the union of multiple sets through two ways.
我们可以通过两种方式创建多个集合的并集。
- By passing multiple sets as argument in union() function. 通过在union()函数中将多个集合作为参数传递。
- 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](https://i-blog.csdnimg.cn/blog_migrate/b1ad0f286a6cb1132410c711dcfc7bd0.png)
Python Multiple Sets Union
Python多集联合
Reference: Official Documentation
参考: 官方文档
python set