python 深拷贝使用
2014-04-09 17:45
357 查看
下面代码不是想要的结果:
以下结果不是我们想要的结果:
未使用拷贝解决上面的问题:
使用拷贝解决上面的问题:
想要的结果:
vals = [{'id':'01','location_id':'a001','number':100},{'id':'02','location_id':'a002','number':200},{'id':'03','location_id':'a003','number':500}] temp_list = [] temp_vals = {} new_list = [] for val in vals: temp_vals['id'] = val['id'] temp_vals['number'] = val['number'] value = [0, False, temp_vals] temp_list.append(value) print temp_list
以下结果不是我们想要的结果:
未使用拷贝解决上面的问题:
# -*- coding: utf-8 -*- vals = [{'id':'01','location_id':'a001','number':100},{'id':'02','location_id':'a002','number':200},{'id':'03','location_id':'a003','number':500}] temp_list = [] temp_vals = {} new_list = [] for val in vals: temp_vals['id'] = val['id'] temp_vals['number'] = val['number'] value = [0, False, temp_vals] temp_list.append(value) #把字典置空 temp_vals = {} print temp_list
使用拷贝解决上面的问题:
import copy vals = [{'id':'01','location_id':'a001','number':100},{'id':'02','location_id':'a002','number':200},{'id':'03','location_id':'a003','number':500}] temp_list = [] temp_vals = {} new_list = [] for val in vals: temp_vals['id'] = val['id'] temp_vals['number'] = val['number'] value = [0, False, temp_vals] new_list = copy.deepcopy(value) temp_list.append(new_list) print temp_list
想要的结果:
相关文章推荐
- Python学习 之 对内存的使用(浅拷贝和深拷贝)
- python学习笔记:深浅拷贝的使用和原理
- Python学习17:使用Python拷贝文本文件
- Python基础——文件拷贝(从手动实现到shutil的使用)
- Python3 - 对内存的使用--深拷贝,浅拷贝
- python使用paramiko实现远程拷贝文件的方法
- Python:使用copy模块深拷贝对象
- python_使用copy模块实现列表(list)拷贝
- Python中使用copy模块实现列表(list)拷贝
- 使用进程池进行大批量文件拷贝实现(Python)
- 浅谈Python对内存的使用(深浅拷贝)
- Python对内存的使用(深拷贝和浅拷贝)
- Python中使用copy模块实现列表(list)拷贝
- python使用paramiko实现远程拷贝文件的方法
- Python使用Paramiko模块实现远程文件拷贝
- python使用pyhook监控键盘并实现切换歌曲的功能
- 使用python设置超时
- 关于Python的一些使用小技巧
- Pydev 的覆盖率测试python coverage以及其他使用
- python项目练习六:使用CGI进行远程编辑