Python之 七级字典查询
1 # -*- coding:utf-8 -*- 2 3 4 # 作业要求: 5 # 打印直辖市,省,市,县,区,街道五级菜单; 6 # 可以一层一层地进入到所有层 7 # 可以退出到上一层 8 # 可随时退出程序 9 10 11 mapChina = { 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 }, 52 "很多学校": { 53 "各种学校": "各种湖南小吃", 54 }, 55 }, 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 "怀化市": {}, 81 "湘西少数民族自治区": {}, 82 }, 83 "重庆": {}, 84 "四川": {}, 85 "江西": {}, 86 "陕西": {}, 87 "福建": {}, 88 "上海": {}, 89 "浙江": {}, 90 "其他省市自治区直辖市": {}, 91 } 92 } 93 94 # print(type(mapChina["中华人民共和国"])) 95 # print(mapChina["中华人民共和国"]["湖南"]) 96 # print(mapChina["中华人民共和国"]["湖南"]["长沙市"]["长沙县"]["黄花镇"]["泉塘安置小区"]['中联重科']) 97 98 99 choose_list = [] 100 mapChina_new = mapChina 101 102 print("输入‘b’键返回上一层,输入‘q’键退出到初始界面,输入‘qq’直接退出程序。") 103 print(mapChina.keys()) 104 105 106 while True: 107 108 choose = input("请输入需要访问的区域名称: ") 109 if choose == 'qq': 110 break 111 elif choose == 'q': 112 mapChina_new = mapChina 113 continue 114 elif choose == "b": 115 if mapChina_new == mapChina: 116 continue 117 else: 118 mapChina_new = choose_list.pop() 119 for key1 in mapChina_new.keys(): 120 print(key1) 121 elif choose in mapChina_new.keys(): 122 choose_list.append(mapChina_new) 123 mapChina_new = mapChina_new[choose] 124 for key in mapChina_new.keys(): 125 print(key) 126 elif not mapChina_new.values(): 127 print("这是最后一层了,请输入‘b’键返回") 128 continue 129 else: 130 print("Invalid Input.Try Again!")