python3 深複製與淺複製

2021-10-04 03:28:33 字數 3558 閱讀 8532

import json

def deepclone(dictvalue):

if isinstance(dictvalue, list) or isinstance(dictvalue, set):

return [deepclone(v) if type(v).__name__ in ["list", "set", "tuple", "dict"] else v for v in dictvalue]

elif isinstance(dictvalue, dict):

return

elif isinstance(dictvalue, tuple):

return (deepclone(v) if type(v).__name__ in ["list", "set", "tuple", "dict"] else v for k, v in dictvalue)

else:

raise exception("type of value must is in [list, set, tuple, dict] ")

# is 比較的是記憶體位址

# == 比較的是值

a, b = none, none

print(id(a), id(b))

dict1 =

dict2 = dict1

print("dict=>a " + ("淺複製" if dict1 is dict2 else "深複製"))

dict1 =

dict2 = dict1.copy()

print("copy() dict=>a " + ("淺複製" if dict1 is dict2 else "深複製"))

dict1 =

dict2 = json.loads(json.dumps(dict1, ensure_ascii=false), encoding='utf-8')

print("json dict=>a " + ("淺複製" if dict1 is dict2 else "深複製"))

dict1 = , "b": 2}

dict2 = dict1.copy()

print("copy() dict=>a=>a1 " + ("淺複製" if dict1["a"] is dict2["a"] else "深複製"))

dict1 = , "b": 2}

dict2 = deepclone(dict1)

print("deepclone() dict=>a=>a1 " + ("淺複製" if dict1["a"]

is dict2["a"] else "深複製"))

dict1 = , "b": 2}

dict2 = json.loads(json.dumps(dict1, ensure_ascii=false), encoding='utf-8')

print("json dict=>a=>a1 " + ("淺複製" if dict1 is dict2 else "深複製"))

arr1 = [1, 2, 3]

arr2 = arr1

print("list=>item "+("淺複製" if arr1 is arr2 else "深複製"))

arr1 = [1, 2, 3]

arr2 = arr1.copy()

print("copy() list=>item "+("淺複製" if arr1 is arr2 else "深複製"))

arr1 = [1, 2, 3]

arr2 = json.loads(json.dumps(arr1, ensure_ascii=false), encoding='utf-8')

print("json list=>item "+("淺複製" if arr1 is arr2 else "深複製"))

arr1 = [1, 2, 3]

arr2 = deepclone(arr1)

print("deepclone() list=>item "+("淺複製" if arr1 is arr2 else "深複製"))

arr1 = [[1, 2], 2, 3]

arr2 = arr1.copy()

print("copy() list=>list=>item "+("淺複製" if arr1[0] is arr2[0] else "深複製"))

arr1 = [[1, 2], 2, 3]

arr2 = deepclone(arr1)

print("deepclone() list=>list=>item "+("淺複製" if arr1[0] is arr2[0] else "深複製"))

arr1 = [set([1, 2]), 2, 3]

arr2 = arr1.copy()

print("copy() list=>set=>item "+("淺複製" if arr1[0] is arr2[0] else "深複製"))

arr1 = [set([1, 2]), 2, 3]

arr2 = deepclone(arr1)

print("deepclone() list=>set=>item "+("淺複製" if arr1[0] is arr2[0] else "深複製"))

arr1 = [, 2, 3]

arr2 = arr1.copy()

print("copy() list=>dict=>item "+("淺複製" if arr1[0] is arr2[0] else "深複製"))

arr1 = [, 2, 3]

arr2 = deepclone(arr1)

print("deepclone() list=>dict=>item "+("淺複製" if arr1[0] is arr2[0] else "深複製"))

結果

140734991645920 140734991645920

dict=>a 淺複製

copy() dict=>a 深複製

json dict=>a 深複製

copy() dict=>a=>a1 淺複製

deepclone() dict=>a=>a1 深複製

json dict=>a=>a1 深複製

list=>item 淺複製

copy() list=>item 深複製

json list=>item 深複製

deepclone() list=>item 深複製

copy() list=>list=>item 淺複製

deepclone() list=>list=>item 深複製

copy() list=>set=>item 淺複製

deepclone() list=>set=>item 深複製

copy() list=>dict=>item 淺複製

deepclone() list=>dict=>item 深複製

結論

直接賦值是進行淺複製的

copy() 只能進行第一級的深複製

使用json進行序列化後再解析可以實現深複製

json的方式效率不高 推薦使用文中的deepclone 方法

淺複製與深複製

舉個複製 的例子 function clone p,s for var prop in p return s var a var b clone a,b b.hobby 1 reading b.hobby 1 reading c.hobby 1 reading 由上面的例子可以看出,當執行淺複製時,...

深複製與淺複製

2者主要區別 是否為成員變數分配新記憶體空間 複製建構函式有2種 深複製與淺複製 當將乙個物件a複製給另乙個物件b時,系統會呼叫複製建構函式 編譯器預設的複製建構函式是淺拷貝,它將原物件的每乙個成員變數複製到新物件的成員變數中去。因而最後兩個物件的變數均指向相同的資料成員的記憶體位址。當資料成員中沒...

淺複製與深複製

1.淺複製 被複製物件的所有變數都含有與原來的物件相同的值,而其所有的對其他物件的引用都仍然指向原來的物件。說明 乙個物件中的資料成員 include using namespace std class test test const test c 複製建構函式,提供乙個同型別物件作為引數 void...