Python基本数据结构之字典嵌套(例子)
北京城市地区之间的嵌套
1 # coding=gbk
2 #Created on 2019/5/20
3 #@author: XiaoHu
4
5 menu = {
6 '北京': {
7 '朝阳': {
8 '国贸': {
9 'CICC': {},
10 'HP': {},
11 '中国银行': {},
12 'CCTV': {},
13 },
14 '望京': {
15 '陌陌': {},
16 '奔驰': {},
17 '360': {},
18 },
19 '三里屯': {
20 '优衣库': {},
21 'apple': {},
22 },
23 },
24 '昌平': {
25 '沙河': {
26 "老男孩": {},
27 "阿泰包子": {},
28 },
29 '天通苑': {
30 "链家": {},
31 "我爱我家": {},
32 },
33 '回龙观': {},
34 },
35 '海淀': {
36 "五道口": {
37 "谷歌": {},
38 "网易": {},
39 "Sohu": {},
40 "快手": {},
41 },
42 "中关村": {
43 "优酷": {},
44 "爱奇艺": {},
45 "汽车之家": {},
46 "新东方": {},
47 "QQ": {},
48 },
49 },
50 },
51
52 '上海': {
53 "浦东": {
54 "陆家嘴": {
55 "CICC": {},
56 "高盛": {},
57 "摩根": {},
58 },
59 "外滩": {},
60 },
61 "闵行": {},
62 "静安": {},
63 },
64
65 '山东': {
66 "济南": {
67
68 },
69 "德州": {
70 "乐林": {
71 "丁坞镇": {},
72 "城区": {},
73 },
74 "平原": {},
75 },
76 "青岛": {},
77 },
78 }
79
80 current_layer = menu
81 parent_layer = menu
82
83 parent_layers = []
84
85 while True:
86 for key in current_layer:
87 print(key)
88 choice = input(">>>:").strip()
89 if len(choice) == 0:continue
90 if choice in current_layer:
91 parent_layers.append(current_layer)
92 current_layer = current_layer[choice]
93 elif choice == "b":
94 if parent_layers:
95 current_layer = parent_layers.pop()
96 else:
97 print("无此项")