Python's data structure is generally divided into two categories:
1. Strings and numbers
2. Lists, tuples, dictionaries, etc.
I. Strings and Numbers
For strings and numbers, assignments (=), shallow copies (copy) and deep copies (deep copy) are meaningless because they always point to the same memory address.
>>> import copy >>> a1 = 100 >>> id(a1) 1426656816 # a1 memory address # assignment >>> a2 = a1 >>> id(a2) 1426656816 # shallow copy >>> a3 = copy.copy(a1) >>> id(a3) 1426656816 # deep copy >>> a4 = copy.deepcopy(a1) >>> id(a4) 1426656816
As shown in the figure:
Lists, tuples, dictionaries, etc.
For lists and dictionaries, assignments (=), shallow copies (copy) and deep copies (deep copy) cause different changes.
2.1. Assignment (=)
names = ['Angle', 'Zous', 'Athena', ['Banana', 'apple']] name2 = names names[1] = "Pear" print(names) print(name2) print("-----------------") names[3][1] = "Dog" print(names) print(name2) print(id(names)) print(id(name2)) # output ['Angle', 'Pear', 'Athena', ['Banana', 'apple']] ['Angle', 'Pear', 'Athena', ['Banana', 'apple']] ----------------- ['Angle', 'Pear', 'Athena', ['Banana', 'Dog']] ['Angle', 'Pear', 'Athena', ['Banana', 'Dog']] 1504387406536 1504387406536 # You can see every change in names, and name2 will change, because their memory addresses are the same.
As shown in the figure:
This is different from strings and numbers. When we define a = 1, b = a, and change a, b will not change. Lists, dictionaries, etc. change because their memory addresses are the same.
a = 1 b = a a = 100 print(a) print(b) # output 100 1
2.2. Shallow copy
import copy names = ['Angle', 'Zous', 'Athena', ['Banana', 'apple']] name2 = copy.copy(names) names[1] = "Zeus" print(names, "names The memory address is{}".format(id(names))) print(name2, "name2 The memory address is{}".format(id(name2))) #output ['Angle', 'Zeus', 'Athena', ['Banana', 'apple']] names The memory address is 176417452744 ['Angle', 'Zous', 'Athena', ['Banana', 'apple']] name2 The memory address is 176416035080 //We shallowly copy ed a name2, and we changed the value of Zous in names to Chinese, but Name2 did not change. Because their memory addresses are different, changing one does not affect the other.
Let's look at the following:
import copy names = ['Angle', 'Zous', 'Athena', ['Banana', 'apple']] name2 = copy.copy(names) names[1] = "Zeus" print(names, "names The memory address is{}".format(id(names))) print(name2, "name2 The memory address is{}".format(id(name2))) names[3][1] = 'Apple' print(names, id(names[3])) print(name2, id(name2[3])) # output ['Angle', 'Zeus', 'Athena', ['Banana', 'apple']] names The memory address is 2306153560776 ['Angle', 'Zous', 'Athena', ['Banana', 'apple']] name2 The memory address is 2306152155528 ['Angle', 'Zeus', 'Athena', ['Banana', 'Apple']] 2306153648968 ['Angle', 'Zous', 'Athena', ['Banana', 'Apple']] 2306153648968 # This time we changed not only the value of the outermost list, but also the value of a list in the list. # The result of this time is that the list of the inner layers has changed accordingly.
We can see clearly from the printed memory address that the memory address of the whole outer list is different, but the memory address of the inner list is the same.
As shown in the figure:
Summary: Shallow copy (x) simply copies the first layer of data types such as lists, and the memory address changes. But the memory address for the underlying data type has not changed.
2.3. Deep copy
Deep copy (x) is actually a new memory address, which stores data after deep copy. It is totally different from the original memory address, including the memory address of the underlying data type.
import copy names = ['Angle', 'Zous', 'Athena', ['Banana', 'apple']] name2 = copy.deepcopy(names) names[1] = "Zeus" print(names, "names The memory address is{}".format(id(names))) print(name2, "name2 The memory address is{}".format(id(name2))) names[3][1] = 'Apple' print(names, id(names[3])) print(name2, id(name2[3])) #output ['Angle', 'Zeus', 'Athena', ['Banana', 'apple']] names The memory address is 237824216776 ['Angle', 'Zous', 'Athena', ['Banana', 'apple']] name2 The memory address is 237824217160 ['Angle', 'Zeus', 'Athena', ['Banana', 'Apple']] 2379824304968 ['Angle', 'Zous', 'Athena', ['Banana', 'apple']] 2379824305032 #As you can see, names does not affect name2 whether it changes the data in the outer or inner lists.
As shown in the figure: