首页 > 代码库 > python基础-(copy)

python基础-(copy)

浅拷贝

import copy
dic = {
    "cpu":[80,],
    "mem":[80,],
    "disk":[80,]
    }
print(brefore:,dic)
new_dic = copy.copy(dic)
new_dic[cpu][0] = 50
print(dic)
print(new_dic)

#brefore: {‘disk‘: [80], ‘mem‘: [80], ‘cpu‘: [80]}
#{‘disk‘: [80], ‘mem‘: [80], ‘cpu‘: [50]}
#{‘disk‘: [80], ‘mem‘: [80], ‘cpu‘: [50]}

 

深拷贝

import copy
dic = {
    "cpu":[80,],
    "mem":[80,],
    "disk":[80,]
    }
print(brefore:,dic)
new_dic = copy.deepcopy(dic)
new_dic[cpu][0] = 50
print(dic)
print(new_dic)

#brefore: {‘disk‘: [80], ‘cpu‘: [80], ‘mem‘: [80]}
#{‘disk‘: [80], ‘cpu‘: [80], ‘mem‘: [80]}
#{‘disk‘: [80], ‘cpu‘: [50], ‘mem‘: [80]}

 

python基础-(copy)