Detailed Python list assignment copy deep copy and shallow copy of five kinds

This article describes the Python list assignment, copy, copy deep and shallow copy Detailed 5 kinds of paper to tell you in great detail, has a certain value for references, a friend in need can refer to the
Overview

Copy this list of questions, seemingly simple copy but it has a lot of knowledge, especially for the novice, but of course things did not go well, such as a list assignment, copy, copy the shallow and deep copy and so convoluted terms in the end there What is the difference and effect?

List assignment

# 定义一个新列表
l1 = [1, 2, 3, 4, 5]
# 对l2赋值
l2 = l1
print(l1)
l2[0] = 100
print(l1)

Sample results:

[1, 2, 3, 4, 5]
[100, 2, 3, 4, 5]

You can see, L1 L2 will also be changed after the changed assignment, seemingly simple "copy", in Python, the list belongs to mutable objects, and replication of mutable objects is actually similar to the list of memory space the C pointer again point to the new variable names, rather than immutable objects such as strings that will create a new memory space assignment when copying. At this point, ie L1 and L2 are the same piece of memory space, then how true copy of it?

Shallow copy

When the list of elements is immutable objects, we can assign values ​​to the list in the following ways:

import copy
# 定义一个新列表
L0 = [1, 2, 3, 4, 5]
print(L0)
print('-'*40)

Use sliced

L1 = L0[:]
L1[0] = 100
print(L0)

Use module copy

import copy
L2 = copy.copy(L0)
L2[0] = 100
print(L0)

Use list ()

L3 = list(L0)
L3[0] = 100
print(L0)

Using the list of methods extend

L4 = []
L4.extend(L0)
L4[0] = 100
print(L0)

Use list comprehensions

L5 = [i for i in L0]
L5[0] = 100
print(L0)

You can see the final print result is [1, 2, 3, 4, 5], we have successfully carried out a copy of the list, but the conditions need to be in the list of elements to be immutable objects? Because if the elements in the list is a variable object has a reference to an object occur, rather than creating a new memory space referenced, such as when copying:

L0 = [1, 2, [3], 4, 5]
print(L0)
L2 = L0[:]
L2[2][0] = 100
print(L0)

Sample results:

[1, 2, [3], 4, 5]
[1, 2, [100], 4, 5]

Can be seen that, when the object contains a variable list L0, L1 of the replication wherein the variable element to change L2 [2], the variable in the object L0 L0 [2] also changed, so how to achieve true full copy of it?

Deep copy

Using the copy module copies deep deepcopy:

import copy
L0 = [1, 2, [3], 4, 5]
print(L0)
L2 = copy.deepcopy(L0)
L2[2][0] = 100
print(L2)
print(L0)

Sample results:

[1, 2, [100], 4, 5]
[1, 2, [3], 4, 5]

Our recommended python learning sites to see how seniors are learning! From basic python script, reptiles, django, data mining, programming techniques, as well as to combat zero-based sorting data items, given to every love learning python small partner! Python veteran day have to explain the timing of technology, to share some of the ways to learn and need to pay attention to small details, click to join our gathering python learner

Summary
above is a Python list Detailed Xiaobian to introduce deep copy of the assignment and five kinds of shallow copy copy, we want to help

Published 49 original articles · won praise 63 · views 60000 +

Guess you like

Origin blog.csdn.net/haoxun05/article/details/104545971