The depth of python entry

a1=["a","b","c","aa"] 
b1=a1
a1[0]="1"
print(a1,b1)
The result is:
a1=["1","b","c","aa"]
b1=["1","b","c","aa"] 

When two lists are assigned to wait, change the elements in one of the lists, and the elements in the other list will also change.


a1=["a","b","c","aa"]
c1=a1.copy()
a1[0]="1"
print(a1,c1)
此时结果为:
a1=["1","b","c","aa"]
b1=["a","b","c","aa"]


a1=["a","b","c","aa",["gaohui",21]]
d1=a1.copy()
a1[-1][0]="hong"
print(a1,d1)
此时结果为:
a1=["a","b","c","aa",["hong",21]]
d1=["a","b","c","aa",["gaohui",21]]

Shallow copy:
1. The list itself is independent, and the elements in it are shared by the two lists.
2. When the elements in one of the lists are changed, the elements in the other list will not change.
3. When there is a list in the list, when the content of the internal list is modified, the internal list in another list will also change accordingly

Deep copy:
import copy
list=copy.deepcopy(list)
When deep copy is used, all the contents of the two lists are independent and have nothing to do with each other.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324927936&siteId=291194637