2022年 11月 9日

python深复制浅复制_Python复制–深层复制

python深复制浅复制

Today we will learn about Python copy class. We will also learn about shallow copy, deep copy and and why do we need these different copy operations.

今天,我们将学习Python复制类。 我们还将学习浅拷贝,深拷贝以及为什么我们需要这些不同的拷贝操作。

Python复制 (Python Copy)

Python library provides us a Copy class with two operations – copy(x) and deepcopy(x) – for shallow copy and deep copy respectively. But why do we need them? Why can’t simple assignment of one object to another using equals operator is good enough?

Python库为Copy类提供了两个操作– copy(x)deepcopy(x) –分别用于浅拷贝和深拷贝。 但是为什么我们需要它们? 为什么不能使用equals运算符简单地将一个对象分配给另一个对象就足够了?

The reason is that when we use equals python operator to create a new object, it actually creates a reference to the same object in the memory. Since both the variables refer to same object, any change in one will be reflected in other too. Most of the times we don’t want that, hence the need of a separate copy operation. Let’s prove our theory about assignment operator through a simple example.

原因是当我们使用equals python运算符创建新对象时,它实际上创建了对内存中同一对象的引用。 由于两个变量都指向同一对象,因此一个变量的任何更改也将反映在另一个变量上。 大多数时候我们不想要那样,因此需要单独的复制操作。 让我们通过一个简单的例子来证明关于赋值运算符的理论。

  1. old_list = [1, 2, 3, 4]
  2. new_list = old_list
  3. new_list.append(5)
  4. print("new_list items : ", new_list)
  5. print("old_list items : ", old_list)

Output of above python program is:

上面的python程序的输出是:

  1. new_list items : [1, 2, 3, 4, 5]
  2. old_list items : [1, 2, 3, 4, 5]

Notice that we didn’t changed old_list but since both the lists were pointing to same object, any change in one of them got reflected in other too.

请注意,我们没有更改old_list,但是由于两个列表都指向同一对象,因此其中一个的任何更改也会反映在另一个对象上。

Also if we have an immutable object then assignment operator is good enough as the object value will not change.

同样,如果我们有一个不变的对象,那么赋值运算符就足够了,因为对象值不会改变。

浅拷贝 (Shallow Copy)

When we use python shallow copy function copy(), it actually creates a new object and then insert references of the objects found in the original object. So shallow copy is good enough in above case where we have a list of integers because the list elements are immutable. But it’s not good enough when we have a list of list. We will look into deep copy later, let’s first look at the python shallow copy example.

当我们使用python浅表复制函数copy() ,它实际上会创建一个新对象,然后插入在原始对象中找到的对象的引用。 因此,在上面有整数列表的情况下,浅表复制就足够了,因为列表元素是不可变的。 但是,当我们有列表列表时,这还不够好。 稍后我们将研究深度复制,让我们首先看一下python浅表复制示例。

  1. import copy
  2. old_list = [1, 2, 3, 4]
  3. # python copy - shallow copy
  4. new_list = copy.copy(old_list)
  5. new_list.append(5)
  6. print("new_list items : ", new_list)
  7. print("old_list items : ", old_list)

As you can see in above output that the change in new_list didn’t affected the old_list because we use copy function to copy the list.

如您在上面的输出中看到的,由于我们使用复制功能复制列表,因此new_list的更改未影响old_list

Now let’s see an example where shallow copy operation will fail because it doesn’t copy elements in the original object recursively.

现在让我们看一个例子,其中浅拷贝操作将失败,因为它不会递归地复制原始对象中的元素。

  1. import copy
  2. old_list = [[1, 2], [3, 4]]
  3. new_list = copy.copy(old_list)
  4. new_list[0].append(10)
  5. print("new_list items : ", new_list)
  6. print("old_list items : ", old_list)

Output of above python copy example is:

上面的python复制示例的输出是:

  1. new_list items : [[1, 2, 10], [3, 4]]
  2. old_list items : [[1, 2, 10], [3, 4]]

The output clearly suggests that we need a separate operation for deep copy of objects.

输出清楚地表明我们需要对对象的深层复制进行单独的操作。

Python深度复制 (Python Deep Copy)

We should always use deepcopy(x) function with objects like list of lists, so that objects are copied recursively. Let’s change the above shallow copy example and use deep copy function and check the output. I have also added some more append and remove operations to the program.

我们应该始终对诸如列表列表之类的对象使用deepcopy(x)函数,以便以递归方式复制对象。 让我们更改上述浅拷贝示例,并使用深拷贝功能并检查输出。 我还向该程序添加了更多附加和删除操作。

  1. import copy
  2. list_of_list = [[1, 2], [3, 4], ["A", "B"]]
  3. # python deep copy
  4. new_list_of_list = copy.deepcopy(list_of_list)
  5. new_list_of_list[0].append(10)
  6. new_list_of_list[1].remove(3)
  7. list_of_list[2].append("C")
  8. print("list_of_list items : ", list_of_list)
  9. print("new_list_of_list items : ", new_list_of_list)

Below image shows the output of python deep copy operation.

下图显示了python深度复制操作的输出。

Note that this method is slower than shallow copy for obvious reasons, so use it only when it’s really required. Also deep copy will take more memory if the objects inside are also copied, so use it wisely and only if truly required.

请注意,由于明显的原因,此方法比浅表复制要慢,因此仅在确实需要时才使用它。 此外,如果还复制了内部对象,则深层复制将占用更多内存,因此请明智地使用它,并且仅在确实需要时才使用它。

That’s all about python copy and python deep copy operations.

这就是关于python复制和python深度复制操作的全部内容。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/16403/python-copy-deep-copy

python深复制浅复制