forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.py
More file actions
51 lines (37 loc) · 1.14 KB
/
tree.py
File metadata and controls
51 lines (37 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# encoding: utf-8
"""
@author: zhanghe
@software: PyCharm
@file: tree.py
@time: 2017/5/22 下午6:57
"""
import json
from collections import defaultdict
def tree():
"""
定义一棵树
python 字典的特性,赋值操作必须事先声明,所以这里使用 collections 很方便的为字典设置初始值
:return:
"""
return defaultdict(tree)
if __name__ == '__main__':
users = tree()
users['jack_1']['jack_2_1']['jack_3_1'] = {}
users['jack_1']['jack_2_1']['jack_3_2'] = {}
users['jack_1']['jack_2_2'] = {}
users['jack_1']['jack_2_2']['jack_3_1'] = {}
users['lily_1']['lily_2_1']['lily_3_1'] = {}
users['lily_1']['lily_2_2']['lily_3_2'] = {}
users['lily_1']['lily_2_3']['lily_3_3'] = {}
users['emma_1']['emma_2_1'] = {}
# 打印 users 原始结构
print users
# 打印 users json 结构
print(json.dumps(users, indent=4))
# 第一层(users的key)
print [i for i in users]
# 第二层(users子节点的key)
print [i for i in users['jack_1']]
# 第三层(users孙节点的key)
print [i for i in users['jack_1']['jack_2_1']]