The Flat Dictionary
来源:互联网 发布:python celery 教程 编辑:程序博客网 时间:2024/06/05 00:43
#coding=utf-8def flatten(dictionary): stack = [((), dictionary)] result = {} print("stack: ",stack) while stack: # stack被清空 path, current = stack.pop() if not current: result["/".join((path))] = "" for k, v in current.items(): # 判断v是否是字典 if isinstance(v, dict): # (path + (k,))合并成一个元组,存储新的路径 stack.append((path + (k,), v)) else: result["/".join((path + (k,)))] = v print("(path + (k,)) : ",path + (k,)) return resultif __name__ == '__main__': #These "asserts" using only for self-checking and not necessary for auto-testing assert flatten({"key": "value"}) == {"key": "value"}, "Simple" assert flatten( {"key": {"deeper": {"more": {"enough": "value"}}}} ) == {"key/deeper/more/enough": "value"}, "Nested" assert flatten({"empty": {}}) == {"empty": ""}, "Empty value" assert flatten({"name": { "first": "One", "last": "Drone"}, "job": "scout", "recent": {}, "additional": { "place": { "zone": "1", "cell": "2"}}} ) == {"name/first": "One", "name/last": "Drone", "job": "scout", "recent": "", "additional/place/zone": "1", "additional/place/cell": "2"}
0 0
- checkio-the flat dictionary
- The Flat Dictionary
- The world is flat
- Notes on the Flat-Text Transcription
- The Battle Between Flat Design And Skeuomorphism
- 扁平设计时代-The Flat Design Era
- Python: The Dictionary Playbook
- 《世界是平的》The World Is Flat
- Updating the flat file of an iSCSI target in AIX
- The Internet is Flat: Modeling the Transition from a Transit Hierarchy to a Peering Mesh
- VMware Convert Error: The size of all flat source disks will exceed the capacity of xxxxx
- The International Dictionary of Artificial Intelligence
- View statistics in the data dictionary
- The Longman Active Study Dictionary CD
- The Relational Database Dictionary, Extended Edition
- Modify the value of Dictionary in circle
- Dictionary access in Python the right way
- The Machine Learning Dictionary
- alarm和pause函数
- mysql数据库的插入(insert)和删除(delete)操作
- 15
- 堆实际应用
- Codeforces-678C-Joty and Chocolate
- The Flat Dictionary
- Oracle安装
- 第一行代码--android学习笔记1-----活动的基本用法
- 剑指offer:链表中倒数第k个结点
- 机器学习开放性问题考点
- 16
- maven 亲测可用国内镜像 阿里云
- Gson在java对象和json字符串之间转换
- 再次认识mysql(七)存储过程