Python 简明教程

Python - Set Operators

Set Operators in Python

Python 中的 set 运算符是特殊的符号和函数,它们允许你在集合上执行各种操作,例如并集、交集、差集和对称差集。这些运算符提供了一种组合、比较和修改集合的方法。

The set operators in Python are special symbols and functions that allow you to perform various operations on sets, such as union, intersection, difference, and symmetric difference. These operators provide a way to combine, compare, and modify sets.

Python 使用以下集合运算符来实现它们 −

Python implements them with following set operators −

Python Set Union Operator (|)

两个集合的并集是一个包含在 A 或 B 或者既在 A 中又在 B 中的所有不重复元素的集合。例如,

The union of two sets is a set containing all distinct elements that are in A or in B or both. For example,

{1,2}∪{2,3}={1,2,3}

下图展示了两个集合的并集。

The following diagram illustrates the union of two sets.

union of two sets

在 Python 中,你可以使用 union() 函数或 | 运算符执行并集运算。此操作组合两个集合的元素,同时消除重复项,从而产生一个新的集合,其中包含来自两个集合的所有唯一元素 −

In Python, you can perform the union operation using the union() function or the | operator. This operation combines the elements of two sets while eliminating duplicates, resulting in a new set containing all unique elements from both sets −

Example

以下示例使用“|”运算符和 union() 函数,并返回两个集合的并集 −

The following example uses the "|" operator and union() function, and returns the union of two sets −

set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = {6, 8, 9}
set4 = {9, 45, 73}
union_set1 = set1.union(set2)
union_set2 = set3 | set4
print ('The union of set1 and set2 is', union_set1)
print ('The union of set3 and set4 is', union_set2)

执行上面的代码后,我们得到以下输出: -

After executing the above code, we get the following output −

The union of set1 and set2 is {1, 2, 3, 4, 5}
The union of set3 and set4 is {73, 6, 8, 9, 45}

Python Set Intersection Operator (&)

两个集合 AA 和 BB 的交集,表示为 A∩B,由 A 和 B 中共有的所有元素组成。例如,

The intersection of two sets AA and BB, denoted by A∩B, consists of all elements that are common to both in A and B. For example,

{1,2}∩{2,3}={2}

下图展示了两个集合的交集。

The following diagram illustrates intersection of two sets.

intersection operator

Python 提供了 intersection() 函数或 & 运算符执行此操作。结果集仅包含两个集合中都存在的元素 -

Python provides the intersection() function or the & operator to perform this operation. The resulting set contains only the elements present in both sets −

Example

以下示例使用 & 运算符和 intersection() 函数,并返回两个集合的交集 -

Following example uses & operator and intersection() function, and returns intersection of two sets −

set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = {6, 8, 9}
set4 = {9, 8, 73}
intersection_set1 = set1.intersection(set2)
intersection_set2 = set3  & set4
print ('The intersection of set1 and set2 is', intersection_set1)
print ('The intersection of set3 and set4 is', intersection_set2)

它将生成如下输出:

It will produce the following output −

The intersection of set1 and set2 is {3}
The intersection of set3 and set4 is {8, 9}

Python Set Difference Operator (-)

两个集合之间的差(减法)包括出现在第一个集合中但不在第二个集合中的元素。它的定义如下。集合 A-B 包含出现在 A 中但不在 B 中的元素。例如,

The difference (subtraction) between two sets consists of elements present in the first set but not in the second set. It is defined as follows. The set A−B consists of elements that are in A but not in B. For example,

If A={1,2,3} and B={3,5}, then A−B={1,2}

下图说明了两个集合之间的差 -

The following diagram illustrates difference of two sets −

difference operator

Python 提供了 difference() 函数或 - 运算符执行此操作。结果集包含第一个集合中唯一的元素 -

Python provides the difference() function or the - operator to perform this operation. The resulting set contains elements unique to the first set −

Example

以下示例使用“ -”运算符和 difference() 函数,并返回两个集合之间的差 -

The following example uses the "-" operator and the difference() function, and returns difference of two sets −

set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = {6, 8, 9}
set4 = {9, 8, 73}
difference_set1 = set1.difference(set2)
difference_set2 = set3 - set4
print ('The difference between set1 and set2 is', difference_set1)
print ('The difference between set3 and set4 is', difference_set2)

我们得到了如下输出 −

We get the output as shown below −

The difference between set1 and set2 is {1, 2}
The difference between set3 and set4 is {6}

请注意,“s1-s2”与“s2-s1”不同。

Note that "s1-s2" is not the same as "s2-s1".

Python Set Symmetric Difference Operator

两个集合的对称差包含在任一集合中但不在两个集合中的元素。A 和 B 的对称差表示为“A Δ B”,并由下式定义 -

The symmetric difference of two sets consists of elements that are present in either set but not in both sets. The symmetric difference of A and B is denoted by "A Δ B" and is defined by −

A Δ B = (A − B) ⋃ (B − A)

如果 A = {1, 2, 3, 4, 5, 6, 7, 8} 且 B = {1, 3, 5, 6, 7, 8, 9},则 A Δ B = {2, 4, 9}。

If A = {1, 2, 3, 4, 5, 6, 7, 8} and B = {1, 3, 5, 6, 7, 8, 9}, then A Δ B = {2, 4, 9}.

下图说明了两个集合之间的对称差 -

The following diagram illustrates the symmetric difference between two sets −

symmetric difference

Python 提供了 symmetric_difference() 函数或 ^ 运算符执行此操作。结果集包含每个集合中唯一的元素。

Python provides the symmetric_difference() function or the ^ operator to perform this operation. The resulting set contains elements that are unique to each set.

Example

以下示例使用 ^ 运算符和 symmetric_difference() 函数,并返回两个集合的对称差 -

The following example uses the "^" operator and the symmetric_difference() function, and returns symbolic difference of two sets −

set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = {6, 8, 9}
set4 = {9, 8, 73}
symmetric_difference_set1 = set1.symmetric_difference(set2)
symmetric_difference_set2 = set3 ^ set4
print ('The symmetric difference of set1 and set2 is', symmetric_difference_set1)
print ('The symmetric difference of set3 and set4 is', symmetric_difference_set2)

产生的结果如下 −

The result produced is as follows −

The symmetric difference of set1 and set2 is {1, 2, 4, 5}
The symmetric difference of set3 and set4 is {73, 6}

Python Subset Testing Operation

你可以使用 issubset() 函数或 运算符检查一个集合是否为另一个集合的子集。如果 A 的所有元素也都出现在 B 中,则集合 A 被认为是另一个集合 B 的子集 -

You can check whether one set is a subset of another using the issubset() function or the operator. A set A is considered a subset of another set B if all elements of A are also present in B

Example

以下示例使用“⇐”运算符和 issubset() 函数,并返回两个集合的子集测试 -

The following example uses the "⇐" operator and the issubset() function, and returns subset testing of two sets −

set1 = {1, 2}
set2 = {1, 2, 3, 4}
set3 = {64, 47, 245, 48}
set4 = {64, 47, 3}
is_subset1 = set1.issubset(set2)
is_subset2 = set3 <= set4
print ('set1 is a subset of set2:', is_subset1)
print ('set3 is a subset of set4:', is_subset2)

产生的结果如下 −

The result produced is as follows −

set1 is a subset of set2: True
set3 is a subset of set4: False