python嵌套dict的深度遍历合并
2015-08-24 19:01
274 查看
dict1 = {1: {2: {3: {4: {}}}}} dict2 = {1: {2: {4: {6: {}}}}} dict3 = {1: {2: {3: {5: {}}}}} def deepSearch(dict1, dict2): for key in dict2.keys(): if key not in dict1.keys(): dict1[key] = dict2[key] else: deepSearch(dict1[key], dict2[key]) for dictT in [dict2, dict3]: deepSearch(dict1, dictT) print(dict1)
利用深度遍历来合并嵌套的dict,
point:若key不同则增加并遍历其它的key,若key相同则进入key对应的value进行遍历。
相关文章推荐
- 在VS中安装Python
- python ftplib模块
- python中的read()、readline()与readlines()
- python 使用ioctl() 获得网卡IP 和MAC地址
- python字符串format函数学习
- python-virtualenv安装方法
- 【Python】[IO编程]文件读写,StringIO和BytesIO,操作文件和目录,序列化
- python没有switch,可以用字典来替代
- python闭包以及装饰器
- 分享python的几个学习资源
- WINDOWS安装python3.X遇到的错误解决方法
- 人生苦短,我学python -- 入门篇
- 【Python】关于递进错误indentationerror
- python数据挖掘领域工具包
- python iterator迭代器、yield、container容器和for循环的联系与区别
- python:安装numpy, Scipy,Matplotlib
- 第一章 Python and NLTK
- python-动态特性学习
- ImportError: No module named 'ConfigParser'
- [python] 爬取网站所有的URL