Dictionary copy

#The copy method returns a new dictionary with the same key-value pairs (a 'shallow 
#copy,' since the values themselves are the same, not copies):

x = {'username': 'admin', 'machines': ['foo', 'bar', 'baz']}
y = x.copy()
y['username'] = 'mlh'
y['machines'].remove('bar')
print y

print x
from copy import deepcopy
d = {}
d['names'] = ['Alfred', 'Bertrand']
c = d.copy()
dc = deepcopy(d)
d['names'].append('Clive')
print c
print dc

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图