知识点:浅拷贝-->只拷贝第一层 ; 深拷贝-->类似克隆
1.变量赋值 指针的指向完全一样
列表,字典的内存空间改变,不会重新开辟一个内存空间,所有指针还是一样,两个值都会改变
a = [[1,2],3,4]
>>> b = a
>>> a
[[1, 2], 3, 4]
>>> b
[[1, 2], 3, 4]
>>> b[1] =3333 //修改b的值,a也会放生变化
>>> b
[[1, 2], 3333, 4]
>>> a
[[1, 2], 3333, 4]
字符串,整数型等内存空间改变,会重新开辟一个内存空间存放对象,所以会抹除原来指针,指向新的对象。两个值没关联
>>> a=1
>>> a = 1
>>> b = a
>>> b = 111
>>>
>>> a
1
>>> b
111
2.浅拷贝
>>> a = [[1,2],3,4]
>>>
>>> b = a.copy()
>>>
>>> a
[[1, 2], 3, 4]
>>> b
[[1, 2], 3, 4]
>>>
>>> b[1] = 333 //对第一层修改,两者独立
>>> b
[[1, 2], 333, 4]
>>> a
[[1, 2], 3, 4]
>>> b[0][0] = 1111 //对非第一层修改,有关联
>>> b
[[1111, 2], 333, 4]
>>> a
[[1111, 2], 3, 4]
3.深拷贝 两者互相没有任何关联
>>> import copy
>>>
>>> a = [[1,2],3,4]
>>> b = copy.deepcopy(a)
>>> a
[[1, 2], 3, 4]
>>> b
[[1, 2], 3, 4]
>>>
>>> a[2] = 444
>>> a
[[1, 2], 3, 444]
>>> b
[[1, 2], 3, 4]
>>> b[0][0] = 111
>>> b
[[111, 2], 3, 4]
>>> a
[[1, 2], 3, 444]