Python树结构之一行代码实现
2016-05-10 16:14
906 查看
用法
from collections import defaultdict def tree(): return defaultdict(tree) #or tree = lambda: defaultdict(tree)
示例1
users = tree() users['codingpy']['username'] = 'earlgrey' users['python']['username'] = 'Guido van Rossum'
运行结果(print(json.dumps(users))
)
{"codingpy": {"username": "earlgrey"}, "python": {"username": "Guido van Rossum"}}
示例2
categories = tree() categories['Programming Languages']['Python'] categories['Python']['Standard Library']['sys'] categories['Python']['Standard Library']['os']
运行结果(print(json.dumps(users))
)
{"Python": {"Standard Library": {"sys": {}, "os": {}}}, "Programming Languages": {"Python": {}}}
原文链接
相关文章推荐
- Python全局变量
- [python网络编程]使用scapy修改源IP发送请求
- python automated headers 自动生成格式化 注释文件 docstrings
- thrift系列 - 多语言实例 for java和python
- [LeetCode]题解(python):149-Max Points on a Line
- 《统计学习方法》-逻辑回归笔记和python源码
- [LeetCode]题解(python):148-Sort List
- Python介绍
- Python内建的对象列表
- [LeetCode]题解(python):147-Insertion Sort List
- [LeetCode]题解(python):146-LRU Cache
- Python处理Excel(四):自定义类处理Excel数据
- python面向对象编程基础II(类的继承)
- Python基础篇【第8篇】: Socket编程(二)SocketServer
- Python实现欧拉计划22题
- python 多个关键字替换
- 102. Binary Tree Level Order Traversal [easy] (Python)
- Python详说正则表达式(对常用的关键字符的讲解)
- Python学习笔记:集合(set)基本内容
- [LINK]用Python计算昨天、今天和明天的日期时间