Python 简明教程
Python - Join Sets
在 Python 中, set 是项的有序集合。项可能属于不同类型。但是,集合中的项必须是不可变对象。这意味着我们只能在集合中包含数字、字符串和元组,而不能包含 lists 。Python 的集合类具有用于连接集合对象的各种条款。
In Python, a set is an ordered collection of items. The items may be of different types. However, an item in the set must be an immutable object. It means, we can only include numbers, string and tuples in a set and not lists. Python’s set class has different provisions to join set objects.
Join Sets in Python
在 Python 中连接集合是指将两个或多个集合合并为一个单独的集合。连接集合时,您会合并多个集合的元素,同时确保删除重复元素,因为集合不允许重复元素。
Joining sets in Python refers to merging two or more sets as a single set. When you join sets, you merge the elements of multiple sets while ensuring that duplicate elements are removed, as sets do not allow duplicate elements.
这可以通过各种方法实现,例如并集、更新、集合推导、集合连接、复制和迭代添加。
This can be achieved using various methods, such as union, update, set comprehension, set concatenation, copying, and iterative addition.
Join Python Sets Using "|" Operator
"|" 符号(管道)被定义为并集运算符。它执行 A∪B 操作,并返回 A、B 或两者中的一组项。集合不允许重复项。
The "|" symbol (pipe) is defined as the union operator. It performs the A∪B operation and returns a set of items in A, B or both. Set doesn’t allow duplicate items.
Example
在下面的示例中,我们对集合 "s1" 和 "s2" 使用 "|" 运算符执行并集操作,创建一个新的集合 "s3",其中包含这两个集合的元素,且不重复 −
In the following example, we are performing a union operation on sets "s1" and "s2" using the "|" operator, creating a new set "s3" containing elements from both sets without duplicates −
s1={1,2,3,4,5}
s2={4,5,6,7,8}
s3 = s1|s2
print (s3)
它将生成如下输出:
It will produce the following output −
{1, 2, 3, 4, 5, 6, 7, 8}
Join Python Sets Using union() Method
集合类具有 union() 方法,该方法执行的操作与 | 运算符相同。它返回一个集合对象,其中包含这两个集合中的所有项,并丢弃重复项。
The set class has union() method that performs the same operation as | operator. It returns a set object that holds all items in both sets, discarding duplicates.
Example
在此示例中,我们在集合 "s1" 上调用 union() 方法,并将集合 "s2" 作为参数传递,它将返回一个新集合 "s3",其中包含这两个集合的元素,且不重复 −
In this example, we are invoking the union() method on set "s1", passing set "s2" as an argument, which returns a new set "s3" containing elements from both sets without duplicates −
s1={1,2,3,4,5}
s2={4,5,6,7,8}
s3 = s1.union(s2)
print (s3)
以下是所获得的输出 −
Following is the output obtained −
{1, 2, 3, 4, 5, 6, 7, 8}
Join Python Sets Using update() Method
update() 方法也会连接两个集合,与 union() 方法一样。但是它不会返回一个新的集合对象。相反,它将第二个集合的元素添加到第一个集合中,不允许重复项。
The update() method also joins the two sets, as the union() method. However it doen’t return a new set object. Instead, the elements of second set are added in first, duplicates not allowed.
Example
在下面的示例中,我们使用 update() 方法用集合 "s2" 的元素更新集合 "s1",将 "s1" 修改为包含这两个集合的元素,且不重复 −
In the example below, we are updating set "s1" with the elements of set "s2" using the update() method, modifying "s1" to contain elements from both sets without duplicates −
s1={1,2,3,4,5}
s2={4,5,6,7,8}
s1.update(s2)
print (s1)
获得的结果如下所示 −
The result obtained is as shown below −
{1, 2, 3, 4, 5, 6, 7, 8}
Join Python Sets Using Unpacking Operator
在 Python 中,"*" 符号用作解包运算符。解包运算符在内部将集合中的每个元素分配给一个单独的变量。
In Python, the "*" symbol is used as unpacking operator. The unpacking operator internally assign each element in a collection to a separate variable.
我们可以通过解包多个集合的元素到新的集合中,使用解包运算符(*)来连接 Python 集合。
We can join Python sets using the unpacking operator (*) by unpacking the elements of multiple sets into a new set.
Example
在下面的示例中,我们通过在集合字面中使用 ^ 运算符,解包集合 s1 和 s2 的元素来新建一个集合 s3。
In the following example, we are creating a new set "s3" by unpacking the elements of sets "s1" and "s2" using the * operator within a set literal −
s1={1,2,3,4,5}
s2={4,5,6,7,8}
s3 = {*s1, *s2}
print (s3)
输出结果如下:
The output produced is as follows −
{1, 2, 3, 4, 5, 6, 7, 8}
Join Python Sets Using Set Comprehension
Python 中的集合解析是使用可迭代元素创建集合的一种简明方式,类似于列表解析,但结果是一个集合而不是一个列表。它允许你通过对可迭代元素中的每一项应用表达式,同时可以选择基于条件来过滤这些项,从而生成集合。
Set comprehension in Python is a concise way to create sets using an iterable, similar to list comprehension but resulting in a set instead of a list. It allows you to generate sets by applying an expression to each item in an iterable while optionally filtering the items based on a condition.
我们可以通过迭代多个集合,将它们的元素添加到一个新的集合中,来使用集合解析连接 Python 集合。
We can join python sets using set comprehension by iterating over multiple sets and adding their elements to a new set.
Example
在这个示例中,我们使用集合解析创建了一个新的集合 joined_set。通过迭代包含 set1 和 set2 的列表,然后迭代每个集合 s 中的每个元素 x,我们将来自这两个集合的所有元素合并到 joined_set 中。
In this example, we are creating a new set "joined_set" using a set comprehension. By iterating over a list containing "set1" and "set2", and then iterating over each element "x" within each set "s", we merge all elements from both sets into "joined_set" −
set1 = {1, 2, 3}
set2 = {3, 4, 5}
joined_set = {x for s in [set1, set2] for x in s}
print(joined_set)
以上代码的输出如下所示 −
Output of the above code is as shown below −
{1, 2, 3, 4, 5}
Join Python Sets Using Iterative Addition
在集合中,迭代添加是指使用循环或迭代构造,将一个集合的元素迭代地添加到另一个集合中。这让你可以合并多个集合的元素到一个集合中,同时确保不会包含重复的元素。
Iterative addition in the context of sets refers to iteratively adding elements from one set to another set using a loop or iteration construct. This allows you to merge the elements of multiple sets into a single set, ensuring that duplicate elements are not included.
我们可以通过迭代每个集合的元素并将它们添加到一个新的集合中,来使用迭代添加来连接 Python 集合。
We can join python sets using iterative addition by iterating over the elements of each set and adding them to a new set.
Example
在下面的示例中,我们首先初始化一个空集合。然后,我们分别迭代 set1 和 set2 中的每个元素,使用 add() 方法将每个元素添加到一个名为 joined_set 的新集合中。
In the example below, we first initialize an empty set. Then, we iterate over each element in "set1" and "set2" separately, adding each element into a new set named "joined_set" using the add() method −
set1 = {1, 2, 3}
set2 = {3, 4, 5}
# Initializing an empty set to hold the merged elements
joined_set = set()
# Iterating over set1 and adding its elements to the joined set
for element in set1:
joined_set.add(element)
# Iterating over set2 and adding its elements to the joined set
for element in set2:
joined_set.add(element)
print(joined_set)
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −
{1, 2, 3, 4, 5}