python中的深拷贝与浅拷贝
生活随笔
收集整理的這篇文章主要介紹了
python中的深拷贝与浅拷贝
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
淺拷貝的時候,修改原來的對象,深拷貝的對象不會發生改變。
對象的賦值
對象的賦值實際上是對象之間的引用:當創建一個對象,然后將這個對象賦值給另外一個變量的時候,python并沒有拷貝這個對象,而只是拷貝了這個對象的引用。
aList = ["kel","abc",123] print(aList, id(aList)) bList = aListbList.append("add")print(aList, id(aList)) print(bList, id(bList)) (['kel', 'abc', 123], 139637569314688) (['kel', 'abc', 123, 'add'], 139637569314688) (['kel', 'abc', 123, 'add'], 139637569314688)同樣 numpy 下的數據結構與數據類型的轉換(np.array vs. np.asarray)
np.array() 是深拷貝,np.asarray() 是淺拷貝
兩者主要的區別在于,array(默認)復制一份對象,asarray不會執行這一動作。
def asarray(a, dtype=None, order=None):return array(a, dtype, copy=False, order=order)示例一
import numpy as np arr1=np.ones((3,3)) arr2=np.array(arr1) arr3=np.asarray(arr1) print(arr2 is arr1) print(arr3 is arr1) print('arr1:',arr1, id(arr1)) print('arr2:',arr2, id(arr2)) print('arr3:',arr3, id(arr3)) False True ('arr1:', array([[ 1., 1., 1.],[ 1., 1., 1.],[ 1., 1., 1.]]), 139637569303856) ('arr2:', array([[ 1., 1., 1.],[ 1., 1., 1.],[ 1., 1., 1.]]), 139637569303776) ('arr3:', array([[ 1., 1., 1.],[ 1., 1., 1.],[ 1., 1., 1.]]), 139637569303856)示例二
import numpy as np arr1=np.ones((3,3)) arr2=np.array(arr1) arr3=np.asarray(arr1) arr1[1]=2 print('arr1:',arr1, id(arr1)) print('arr2:',arr2, id(arr2)) print('arr3:',arr3, id(arr3)) ('arr1:', array([[ 1., 1., 1.],[ 2., 2., 2.],[ 1., 1., 1.]]), 139637569303296) ('arr2:', array([[ 1., 1., 1.],[ 1., 1., 1.],[ 1., 1., 1.]]), 139637569303376) ('arr3:', array([[ 1., 1., 1.],[ 2., 2., 2.],[ 1., 1., 1.]]), 139637569303296)對象的復制
當你想修改一個對象,而且讓原始的對象不受影響的時候,那么就需要使用到對象的復制,對象的復制可以通過三種方法實現:
a、 使用切片操作進行拷貝--slice operation b、 使用工廠函數進行拷貝,list/dir/set--factoryfunction c、 copy.copy()--use copymodule在復制的時候,使用的是淺拷貝,復制了對象,但是對象中的元素,依然使用引用。
person = ["name",["savings",100.00]] hubby = person[:] #切片操作 wifey = list(person) #使用工廠函數[id(x) for x in person,hubby,wifey] print("The person is:", person, id(person)) print("The hubby is:", hubby, id(hubby)) print("The wifey is:", wifey, id(wifey)) ('The person is:', ['name', ['savings', 100.0]], 139637569566984) ('The hubby is:', ['name', ['savings', 100.0]], 139637569544848) ('The wifey is:', ['name', ['savings', 100.0]], 139637569405656) print("The person inside is:", [id(x) for x in person]) print("The hubby inside is:", [id(x) for x in hubby]) print("The wifey inside is:", [id(x) for x in wifey]) ('The person inside is:', [139639860076144, 139637569544344]) ('The hubby inside is:', [139639860076144, 139637569544344]) ('The wifey inside is:', [139639860076144, 139637569544344])hubby[0] = "kel" wifey[0] = "jane" hubby[1][1] = 50.0 print("The person is:", person, id(person)) print("The hubby is:", hubby, id(hubby)) print("The wifey is:", wifey, id(wifey)) ('The person is:', ['name', ['savings', 50.0]], 139637570044992) ('The hubby is:', ['kel', ['savings', 50.0]], 139637569460344) ('The wifey is:', ['jane', ['savings', 50.0]], 139637569406160) print("The person inside is:", [id(x) for x in person]) print("The hubby inside is:", [id(x) for x in hubby]) print("The wifey inside is:", [id(x) for x in wifey]) ('The person inside is:', [139639860076144, 139637569810016]) ('The hubby inside is:', [139637569356104, 139637569810016]) ('The wifey inside is:', [139637569378272, 139637569810016])
在使用淺拷貝的時候,發現引用的id都是相同的,但是字符串的id卻發生了變化,是因為在python中,字符串是不可變的,從而在每次進行修改的時候,都是新建一個對象,從而引用發生了變化。
copy模塊
淺拷貝和深拷貝的操作都可以在copy模塊中找到,其實copy模塊中只有兩個函數可用,copy()進行淺拷貝操作,而deepcopy()進行深拷貝操作
#1 import copy aList = [1,"kel",[1,2,3]] print("The aList is:", aList, id(aList))shadeList = copy.copy(aList) print("The shadeList is:", shadeList, id(shadeList))deepList = copy.deepcopy(aList) print("The deepList is:", deepList, id(deepList))aList[2].append("kel")print("The aList is:", aList, id(aList))print("The shadeList is:", shadeList, id(shadeList))print("The deepList is:", deepList, id(deepList)) ('The aList is:', [1, 'kel', [1, 2, 3]], 139639722291712) ('The shadeList is:', [1, 'kel', [1, 2, 3]], 139639722170344) ('The deepList is:', [1, 'kel', [1, 2, 3]], 139637569586096) ('The aList is:', [1, 'kel', [1, 2, 3, 'kel']], 139639722291712) ('The shadeList is:', [1, 'kel', [1, 2, 3, 'kel']], 139639722170344) ('The deepList is:', [1, 'kel', [1, 2, 3]], 139637569586096)
#2 import copy aList = [1,"kel",[1,2,3]] print("The aList is:", aList, id(aList))shadeList = copy.copy(aList) print("The shadeList is:", shadeList, id(shadeList))deepList = copy.deepcopy(aList) print("The deepList is:", deepList, id(deepList))shadeList[2].append("kel")print("The aList is:", aList, id(aList))print("The shadeList is:", shadeList, id(shadeList))print("The deepList is:", deepList, id(deepList)) ('The aList is:', [1, 'kel', [1, 2, 3]], 139637569846448) ('The shadeList is:', [1, 'kel', [1, 2, 3]], 139637569406520) ('The deepList is:', [1, 'kel', [1, 2, 3]], 139637569407240) ('The aList is:', [1, 'kel', [1, 2, 3, 'kel']], 139637569846448) ('The shadeList is:', [1, 'kel', [1, 2, 3, 'kel']], 139637569406520) ('The deepList is:', [1, 'kel', [1, 2, 3]], 139637569407240)
#3 import copy aList = [1,"kel",[1,2,3]] print("The aList is:", aList, id(aList))shadeList = copy.copy(aList) print("The shadeList is:", shadeList, id(shadeList))deepList = copy.deepcopy(aList) print("The deepList is:", deepList, id(deepList))deepList[2].append("kel") print("The deepList is:", deepList, id(deepList))print("The aList is:", aList, id(aList))print("The shadeList is:", shadeList, id(shadeList)) ('The aList is:', [1, 'kel', [1, 2, 3]], 139637569460776) ('The shadeList is:', [1, 'kel', [1, 2, 3]], 139637569461496) ('The deepList is:', [1, 'kel', [1, 2, 3]], 139637569585592) ('The deepList is:', [1, 'kel', [1, 2, 3, 'kel']], 139637569585592) ('The aList is:', [1, 'kel', [1, 2, 3]], 139637569460776) ('The shadeList is:', [1, 'kel', [1, 2, 3]], 139637569461496)
參考文獻
numpy 下的數據結構與數據類型的轉換(np.array vs. np.asarray)
numpy中array和asarray的區別
python中的深拷貝與淺拷貝
總結
以上是生活随笔為你收集整理的python中的深拷贝与浅拷贝的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Tensor数据相关的运算、函数讲解及与
- 下一篇: 12bit灰度图像映射到8bit显示及p