Python 简明教程
Python - Copy Sets
Python Copy Sets
在 Python 中复制集合是指创建一个新 set ,其中包含与现有集合中相同元素。与简单的变量赋值(它会创建一个指向原始集合的引用)不同,复制能确保对复制的集合所做的更改不会影响原始集合,反之亦然。
Copying sets in Python refers to creating a new set that contains the same elements as an existing set. Unlike simple variable assignment, which creates a reference to the original set, copying ensures that changes made to the copied set do not affect the original set, and vice versa.
在 Python 中,有不同的方法可以复制一个集合,包括使用 copy() 方法、set() 函数或集合解析。
There are different methods for copying a set in Python, including using the copy() method, the set() function or set comprehension.
Copy Sets Using the copy() Method
set 类中的 copy() 方法用于创建集合对象的浅拷贝。
The copy() method in set class is used to create a shallow copy of a set object.
浅拷贝意味着该方法会创建一个新的集合对象,但不会创建原始集合中包含对象的副本。相反,它复制这些对象的引用。
A shallow copy means that the method creates a new collection object, but does not create copies of the objects contained within the original collection. Instead, it copies the references to these objects.
因此,如果原始集合包含可变对象(如列表、字典或其他集合),则对这些对象的修改都会反映在原始集合和复制的集合中。
Therefore, if the original collection contains mutable objects (like lists, dictionaries, or other sets), modifications to these objects will be reflected in both the original and the copied collections.
Return Value
copy() 方法返回一个新的集合,它是现有集合的浅拷贝。
The copy() method returns a new set which is a shallow copy of existing set.
Example
在以下示例中,我们正在创建集合“lang1”的副本,并将其存储在“lang2”中,然后使用 id() 检索这两个集合及其内存地址。
In the following example, we are creating a copy of the set "lang1" and storing it in "lang2", then retrieving both sets and their memory addresses using id().
在向“lang1”中添加一个元素后,我们再次检索这两个集合及其内存地址,以显示“lang1”和“lang2”是独立的副本 −
After adding an element to "lang1", we retrieve both sets and their memory addresses again to show that "lang1" and "lang2" are independent copies −
lang1 = {"C", "C++", "Java", "Python"}
print ("lang1: ", lang1, "id(lang1): ", id(lang1))
lang2 = lang1.copy()
print ("lang2: ", lang2, "id(lang2): ", id(lang2))
lang1.add("PHP")
print ("After updating lang1")
print ("lang1: ", lang1, "id(lang1): ", id(lang1))
print ("lang2: ", lang2, "id(lang2): ", id(lang2))
Output
这将产生以下输出 −
This will produce the following output −
lang1: {'Python', 'Java', 'C', 'C++'} id(lang1): 2451578196864
lang2: {'Python', 'Java', 'C', 'C++'} id(lang2): 2451578197312
After updating lang1
lang1: {'Python', 'C', 'C++', 'PHP', 'Java'} id(lang1): 2451578196864
lang2: {'Python', 'Java', 'C', 'C++'} id(lang2): 2451578197312
Copy Sets Using the set() Function
Python set() 函数用于创建新的集合对象。它接受一个可迭代对象作为参数,并将其转换成一个集合,在此过程中删除所有重复元素。如果没有提供参数,则创建一个空集合。
The Python set() function is used to create a new set object. It takes an iterable as an argument and convert it into a set, removing any duplicate elements in the process. If no argument is provided, it creates an empty set.
我们可以通过将原始集合作为参数传递给 set() 构造函数来使用 set() 函数复制集合。这创建一个新集合,其中包含原始集合的所有元素,从而确保对新集合的任何修改都不会影响原始集合。
We can copy set using the set() function by passing the original set as an argument to the set() constructor. This creates a new set that contains all the elements of the original set, ensuring that any modifications to the new set do not affect the original set.
Example
在这个示例中,我们使用 set() 函数创建“original_set”的副本,并将其存储在“copied_set”中 −
In this example, we are creating a copy of "original_set" using the set() function and storing it in "copied_set" −
# Original set
original_set = {1, 2, 3, 4}
# Copying the set using the set() function
copied_set = set(original_set)
print("copied set:", copied_set)
# Demonstrating that the sets are independent
copied_set.add(5)
print("copied set:",copied_set)
print("original set:",original_set)
Output
以下是上面代码的输出: -
Following is the output of the above code −
copied set: {1, 2, 3, 4}
copied set: {1, 2, 3, 4, 5}
original set: {1, 2, 3, 4}
Copy Sets Using Set Comprehension
集合解析是创建集合中 Python 的简洁方法。它用于通过迭代可迭代对象并有选择地应用条件来过滤元素来生成新集合。该语法与列表解析类似,但使用大括号 {} 而不是方括号 [] −
Set comprehension is a concise way to create sets in Python. It is used to generate a new set by iterating over an iterable and optionally applying conditions to filter elements. The syntax is similar to list comprehension but with curly braces {} instead of square brackets [] −
{expression for item in iterable if condition}
我们可以通过迭代原始集合中的元素并直接使用这些元素创建一个新集合来使用集合解析复制集合。
We can copy sets using set comprehension by iterating over the elements of the original set and directly creating a new set with those elements.
Example
在下面的示例中,我们创建一个名为“original_set”的原始集合,然后使用集合解析将其复制到“copied_set”中 −
In the example below, we create an original set named "original_set", then copy it using set comprehension into "copied_set" −
# Original set
original_set = {1, 2, 3, 4, 5}
# Copying the set using set comprehension
copied_set = {x for x in original_set}
print("Copied set:", copied_set)
Output
以上代码的输出如下所示 −
Output of the above code is as shown below −
Copied set: {1, 2, 3, 4, 5}