浅拷贝:
A = [[1,2],3,4]
B= A.copy()
B[0][1] = 5
print(B,A)
B = [[1,5],3,4]
A = [[1,5],3,4]
B[2]=6
A = [[1,2],3,4]
B =[[1,2],3,6]
深拷贝:
import copy
A = [[1,2],3,4]
B=copy.deepcopy(A)
B[0][1] = 7
B = [[1,7],3,4]
A = [[1,2],3,4]
深浅拷贝的区别:
浅拷贝只拷贝第一层,第二层不拷贝
变量:
A= 2
B = A
B= 3
print(B,A)
B= 3
A =2
变量B相当于从新开辟了一块新的内存,和A不共用
列表:
A= [1,2,3,4]
B =A
B[2] = 5
print(A,B)
A = [1,2,5,4]
B=[1,2,5,4]
列表A和B共用一块内存