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.

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

python set_Python Set联合_第1张图片

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 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 set_Python Set联合_第2张图片

Python Multiple Sets Union

Python多集联合

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

Reference: Official Documentation

参考: 官方文档

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

python set

你可能感兴趣的:(python,机器学习,深度学习,java,大数据)