Python中list的復制及深拷貝與淺拷貝探究
在Python中,經常要對一個list進行復制。對於復制,自然的就有深拷貝與淺拷貝問題。深拷貝與淺拷貝的區別在於,當從原本的list復制出新的list之後,修改其中的任意一個是否會對另一個造成影響,即這兩個list在內存中是否儲存在同一個區域,這也是區分深拷貝與淺拷貝的重要依據。接下來我們就針對Python中list復制的幾種方法,來探究一下其是屬於深拷貝還是淺拷貝。弄清楚這個問題,有助於我們在編程中規避錯誤,減少不必要的調試時間。
一、非拷貝方法——直接賦值
如果用=直接賦值,是非拷貝方法。這兩個列表是等價的,修改其中任何一個列表都會影響到另一個列表。這也是Python作為動態語言與C這類靜態語言在思想上的不同之處。
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 4 old = [1, [1, 2, 3], 3] 5 new = old 6 print(‘Before:‘) 7 print(old) 8 print(new) 9 new[0] = 3 10 new[1][0] = 3 11 print(‘After:‘) 12 print(old) 13 print(new)
運行結果:
二、淺拷貝的幾種方法
1.copy()方法
我們來看以下代碼:
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*-3 4 old = [1, [1, 2, 3], 3] 5 new = old.copy() 6 print(‘Before:‘) 7 print(old) 8 print(new) 9 new[0] = 3 10 new[1][0] = 3 11 print(‘After:‘) 12 print(old) 13 print(new)
運行結果:
對於list的第一層,是實現了深拷貝,但對於嵌套的list,仍然是淺拷貝。這其實很好理解,內層的list保存的是地址,復制過去的時候是把地址復制過去了。嵌套的list在內存中指向的還是同一個。
2.使用列表生成式
使用列表生成式產生新列表也是一個淺拷貝方法,只對第一層實現深拷貝。
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 4 old = [1, [1, 2, 3], 3] 5 new = [i for i in old] 6 print(‘Before:‘) 7 print(old) 8 print(new) 9 new[0] = 3 10 new[1][0] = 3 11 print(‘After:‘) 12 print(old) 13 print(new)
運行結果:
3.用for循環遍歷
通過for循環遍歷,將元素一個個添加到新列表中。這也是一個淺拷貝方法,只對第一層實現深拷貝。
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 4 old = [1, [1, 2, 3], 3] 5 new = [] 6 for i in range(len(old)): 7 new.append(old[i]) 8 print(‘Before:‘) 9 print(old) 10 print(new) 11 new[0] = 3 12 new[1][0] = 3 13 print(‘After:‘) 14 print(old) 15 print(new)
運行結果:
4.使用切片
通過使用[:]切片,可以淺拷貝整個列表。同樣的,只對第一層實現深拷貝。
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 4 old = [1, [1, 2, 3], 3] 5 new = old[:] 6 print(‘Before:‘) 7 print(old) 8 print(new) 9 new[0] = 3 10 new[1][0] = 3 11 print(‘After:‘) 12 print(old) 13 print(new)
運行結果:
三、深拷貝的實現
如果用deepcopy()方法,則無論多少層,無論怎樣的形式,得到的新列表都是和原來無關的,這是最安全最清爽最有效的方法。
使用時,要導入copy。
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 4 import copy 5 6 old = [1, [1, 2, 3], 3] 7 new = copy.deepcopy(old) 8 print(‘Before:‘) 9 print(old) 10 print(new) 11 new[0] = 3 12 new[1][0] = 3 13 print(‘After:‘) 14 print(old) 15 print(new)
運行結果:
以上,就是對list的復制及深拷貝與淺拷貝探究。鑒於本人水平有限,文中有不妥之處,還請在評論中指出。
Python中list的復制及深拷貝與淺拷貝探究