python字典复制(浅拷贝and深拷贝)
来源:互联网 发布:linux acl权限控制 编辑:程序博客网 时间:2024/05/16 16:00
1》
>>> d
{'age': 25, 'name': 'song'}
>>> d1=d#将d中的引用(地址)传给d1,d和d1中存放了相同的地址,指向的是同一个字典。
>>> d
{'age': 25, 'name': 'song'}
>>> d1
{'age': 25, 'name': 'song'}
>>> id(d)
49069920
>>> id(d1)
49069920
>>> id(d['age'])
7107952
>>> id(d1['age'])
7107952
>>> d['age']=21
>>> d
{'age': 21, 'name': 'song'}
>>> d1
{'age': 21, 'name': 'song'}
上述代码可用下图解释:
2》
>>> d={'age': 25, 'name': 'song'}
>>> d1={'age': 25, 'name': 'song'}
>>> d
{'age': 25, 'name': 'song'}
>>> d1
{'age': 25, 'name': 'song'}
>>> id(d)
49249424
>>> id(d1)
49585440
>>> id(d['name'])
49649312
>>> id(d1['name'])
49649312
>>> d['name']='wahaha'#修改字典d
>>> d
{'age': 25, 'name': 'wahaha'}
>>> d1
{'age': 25, 'name': 'song'}
>>> id(d['name'])
49649248
>>> id(d1['name'])
49649312
上述代码可用下图解释:
3》
>>> d={'age':25,'score':[90,99,100]}
>>> d1=d.copy()#浅拷贝
>>> d
{'age': 25, 'score': [90, 99, 100]}
>>> d1
{'age': 25, 'score': [90, 99, 100]}
>>> id(d)
49585152
>>> id(d1)
49585872
>>> id(d['score'])
6829120
>>> id(d1['score'])
6829120
>>> d['score'][2]=80
>>> d
{'age': 25, 'score': [90, 99, 80]}
>>> d1
{'age': 25, 'score': [90, 99, 80]}
上述代码可用下图来解释:
4》
>>> import copy
>>> d={'age':25,'score':[90,99,100]}
>>> d1=copy.copy(d)#浅拷贝
>>> d
{'age': 25, 'score': [90, 99, 100]}
>>> d1
{'age': 25, 'score': [90, 99, 100]}
>>> d['score'][2]=80
>>> d
{'age': 25, 'score': [90, 99, 80]}
>>> d1
{'age': 25, 'score': [90, 99, 80]}
copy.copy(d)与d.copy()都是浅拷贝,效果一样,此处就不画图了。
5》
>>> import copy
>>> d={'age':25,'score':[90,99,100]}
>>> d1=copy.deepcopy(d)
>>> d
{'age': 25, 'score': [90, 99, 100]}
>>> d1
{'age': 25, 'score': [90, 99, 100]}
>>> d['score'][2]=80
>>> d
{'age': 25, 'score': [90, 99, 80]}
>>> d1
{'age': 25, 'score': [90, 99, 100]}
>>> id(d)
51137104
>>> id(d1)
51137536
>>> id(d['score'][1])
38959544
>>> id(d1['score'][1])
38959544
>>> id(d['score'][2])
38959772
>>> id(d1['score'][2])
38959532
>>> d
{'age': 25, 'name': 'song'}
>>> d1=d#将d中的引用(地址)传给d1,d和d1中存放了相同的地址,指向的是同一个字典。
>>> d
{'age': 25, 'name': 'song'}
>>> d1
{'age': 25, 'name': 'song'}
>>> id(d)
49069920
>>> id(d1)
49069920
>>> id(d['age'])
7107952
>>> id(d1['age'])
7107952
>>> d['age']=21
>>> d
{'age': 21, 'name': 'song'}
>>> d1
{'age': 21, 'name': 'song'}
上述代码可用下图解释:
2》
>>> d={'age': 25, 'name': 'song'}
>>> d1={'age': 25, 'name': 'song'}
>>> d
{'age': 25, 'name': 'song'}
>>> d1
{'age': 25, 'name': 'song'}
>>> id(d)
49249424
>>> id(d1)
49585440
>>> id(d['name'])
49649312
>>> id(d1['name'])
49649312
>>> d['name']='wahaha'#修改字典d
>>> d
{'age': 25, 'name': 'wahaha'}
>>> d1
{'age': 25, 'name': 'song'}
>>> id(d['name'])
49649248
>>> id(d1['name'])
49649312
上述代码可用下图解释:
3》
>>> d={'age':25,'score':[90,99,100]}
>>> d1=d.copy()#浅拷贝
>>> d
{'age': 25, 'score': [90, 99, 100]}
>>> d1
{'age': 25, 'score': [90, 99, 100]}
>>> id(d)
49585152
>>> id(d1)
49585872
>>> id(d['score'])
6829120
>>> id(d1['score'])
6829120
>>> d['score'][2]=80
>>> d
{'age': 25, 'score': [90, 99, 80]}
>>> d1
{'age': 25, 'score': [90, 99, 80]}
上述代码可用下图来解释:
4》
>>> import copy
>>> d={'age':25,'score':[90,99,100]}
>>> d1=copy.copy(d)#浅拷贝
>>> d
{'age': 25, 'score': [90, 99, 100]}
>>> d1
{'age': 25, 'score': [90, 99, 100]}
>>> d['score'][2]=80
>>> d
{'age': 25, 'score': [90, 99, 80]}
>>> d1
{'age': 25, 'score': [90, 99, 80]}
copy.copy(d)与d.copy()都是浅拷贝,效果一样,此处就不画图了。
5》
>>> import copy
>>> d={'age':25,'score':[90,99,100]}
>>> d1=copy.deepcopy(d)
>>> d
{'age': 25, 'score': [90, 99, 100]}
>>> d1
{'age': 25, 'score': [90, 99, 100]}
>>> d['score'][2]=80
>>> d
{'age': 25, 'score': [90, 99, 80]}
>>> d1
{'age': 25, 'score': [90, 99, 100]}
>>> id(d)
51137104
>>> id(d1)
51137536
>>> id(d['score'][1])
38959544
>>> id(d1['score'][1])
38959544
>>> id(d['score'][2])
38959772
>>> id(d1['score'][2])
38959532
上述代码可用下面图片解释:
(完)
1 0
- python字典复制(浅拷贝and深拷贝)
- python列表复制(浅拷贝and深拷贝)
- Python-字典 深拷贝&浅拷贝
- python中的复制 浅拷贝 深拷贝
- python复制,浅拷贝,深拷贝理解
- Python 深复制浅复制or深拷贝浅拷贝
- Python深复制浅复制or深拷贝浅拷贝
- Python深复制浅复制or深拷贝浅拷贝
- Python深复制浅复制or深拷贝浅拷贝
- Python深复制浅复制or深拷贝浅拷贝
- 【python系列】字典的浅拷贝和深拷贝
- Python中字典的浅拷贝和深拷贝问题
- Python中的赋值以及深复制和浅复制(深拷贝和浅拷贝)
- Python复制——浅拷贝、深拷贝、切片
- python的复制,深拷贝和浅拷贝
- 深拷贝and浅拷贝
- python深拷贝浅拷贝
- python-深拷贝,浅拷贝
- iOS des加密转码16进制字符串
- ACM--是否大于168--HDOJ 1037--Keep on Truckin'--水
- HDU 1005 Number Sequence[数论]
- Servlet的调用过程和生命周期
- ios开发-获取手机相关信息
- python字典复制(浅拷贝and深拷贝)
- 物联网加密
- HDU 3328 Flipper(栈模拟)
- 剑指offer-6-面试43:n 个骰子的点数()
- deepin 系统中MongoDB的安装
- VC维
- SVN服务器和客户端的安装以及在MyEclipse中的配置
- fatal error C1004unexpected end of file found
- android proguard使用心得和遇到的若干问题以及解决思路