浅拷贝复制指向某个对象的地址(指针),而不复制对象本身,新对象和原对象共享同一内存。
深拷贝会额外创建一个新的对象,新对象跟原对象并不共享内存,修改新对象不会影响到原对象。
赋值其实就是引用了原对象。两者指向同一内存,两个对象是联动的,无论哪个对象发生改变都会影响到另一个。
使用=来复制一个列表,实际上不仅复制了其中的内容,也复制了其内存地址,即引用了原列表。使用id()方法查看内存地址也是一样的。修改其中一个列表,也会直接更改另一个列表。
- #!/usr/bin/env python
- # -*- coding:utf-8 -*-
- if __name__ == '__main__':
- a = ["a", ["b", "c", "d"], ["e", "f"]]
- # 直接复制,即引用列表
- b = a
- print(a)
- print(b)
- # 通过id()查看内存地址,为一样的
- print(id(a), id(b))
- b[0] = "g"
- b[1][0] = "f"
- print(a)
- print(b)
输出:
- ['a', ['b', 'c', 'd'], ['e', 'f']]
- ['a', ['b', 'c', 'd'], ['e', 'f']]
- 2030264836936 2030264836936
- ['g', ['f', 'c', 'd'], ['e', 'f']]
- ['g', ['f', 'c', 'd'], ['e', 'f']]
-
- Process finished with exit code 0
使用for循环进行拷贝,仅第一层为深拷贝,对其它层依然是浅拷贝。
- #!/usr/bin/env python
- # -*- coding:utf-8 -*-
- if __name__ == '__main__':
- a = ["a", ["b", "c", "d"], ["e", "f"]]
- b = []
- # 使用for循环进行拷贝,仅第一层为深拷贝
- for i in a:
- b.append(i)
- print(a)
- print(b)
- b[0] = "g"
- b[1][0] = "f"
- print(a)
- print(b)
输出:
- ['a', ['b', 'c', 'd'], ['e', 'f']]
- ['a', ['b', 'c', 'd'], ['e', 'f']]
- ['a', ['f', 'c', 'd'], ['e', 'f']]
- ['g', ['f', 'c', 'd'], ['e', 'f']]
-
- Process finished with exit code 0
使用切片方法进行拷贝,也仅对第一层为深拷贝,对其它层依然是浅拷贝。
- #!/usr/bin/env python
- # -*- coding:utf-8 -*-
- if __name__ == '__main__':
- a = ["a", ["b", "c", "d"], ["e", "f"]]
- # 使用切片进行拷贝,仅第一层为深拷贝
- b = a[:]
- print(a)
- print(b)
- b[0] = "g"
- b[1][0] = "f"
- print(a)
- print(b)
输出:
- ['a', ['b', 'c', 'd'], ['e', 'f']]
- ['a', ['b', 'c', 'd'], ['e', 'f']]
- ['a', ['f', 'c', 'd'], ['e', 'f']]
- ['g', ['f', 'c', 'd'], ['e', 'f']]
-
- Process finished with exit code 0
使用list.copy()方法进行拷贝,也仅对第一层为深拷贝,对其它层依然是浅拷贝。由于列表中嵌套的列表实际保存的是地址,依然指向同一个内存地址。
- #!/usr/bin/env python
- # -*- coding:utf-8 -*-
-
- if __name__ == '__main__':
- a = ["a", ["b", "c", "d"], ["e", "f"]]
- # 使用list.copy()方法进行拷贝,仅第一层为深拷贝
- b = a.copy()
- print(a)
- print(b)
- b[0] = "g"
- b[1][0] = "f"
- print(a)
- print(b)
输出:
- ['a', ['b', 'c', 'd'], ['e', 'f']]
- ['a', ['b', 'c', 'd'], ['e', 'f']]
- ['a', ['f', 'c', 'd'], ['e', 'f']]
- ['g', ['f', 'c', 'd'], ['e', 'f']]
-
- Process finished with exit code 0
使用copy.copy()方法进行拷贝,也仅对第一层为深拷贝,对其它层依然是浅拷贝。
- #!/usr/bin/env python
- # -*- coding:utf-8 -*-
- import copy
-
- if __name__ == '__main__':
- a = ["a", ["b", "c", "d"], ["e", "f"]]
- # 使用copy.copy()方法进行拷贝,仅第一层为深拷贝
- b = copy.copy(a)
- print(a)
- print(b)
- b[0] = "g"
- b[1][0] = "f"
- print(a)
- print(b)
输出:
- ['a', ['b', 'c', 'd'], ['e', 'f']]
- ['a', ['b', 'c', 'd'], ['e', 'f']]
- ['a', ['f', 'c', 'd'], ['e', 'f']]
- ['g', ['f', 'c', 'd'], ['e', 'f']]
-
- Process finished with exit code 0
使用copy.deepcopy()方法进行拷贝,对所有层均为深拷贝,改变新列表并不会影响到原列表,推荐使用。
- #!/usr/bin/env python
- # -*- coding:utf-8 -*-
- import copy
-
- if __name__ == '__main__':
- a = ["a", ["b", "c", "d"], ["e", "f"]]
- # 使用copy.deepcopy()方法进行拷贝,对所有层均为深拷贝
- b = copy.deepcopy(a)
- print(a)
- print(b)
- b[0] = "g"
- b[1][0] = "f"
- print(a)
- print(b)
输出:
- ['a', ['b', 'c', 'd'], ['e', 'f']]
- ['a', ['b', 'c', 'd'], ['e', 'f']]
- ['a', ['b', 'c', 'd'], ['e', 'f']]
- ['g', ['f', 'c', 'd'], ['e', 'f']]
-
- Process finished with exit code 0