首页 > 技术文章 > 三层列表(两种实现)

zi-Chuan 2019-07-12 16:29 原文

  1 #超low的三层菜单
  2 menu = {
  3     '北京':{
  4         '朝阳':{
  5             '国贸':{
  6                 'CICC':{},
  7                 'HP':{},
  8                 '渣打银行':{},
  9                 'CCTV':{},
 10             },
 11             '望京':{
 12                 '陌陌':{},
 13                 '奔驰':{},
 14                 '360':{},
 15             },
 16             '三里屯':{
 17                 '优衣库':{},
 18                 'apple':{},
 19             },
 20         },
 21         '昌平':{
 22             '沙河':{
 23                 '老男孩':{},
 24                 '阿泰包子':{},
 25             },
 26             '天通苑':{
 27                 '链家':{},
 28                 '我爱我家':{},
 29             },
 30             '回龙观':{},
 31         },
 32         '海淀':{
 33             '五道口':{
 34                 '谷歌':{},
 35                 '网易':{},
 36                 'Sohu':{},
 37                 'Sogo':{},
 38                 '快手':{},
 39             },
 40             '中关村':{
 41                 "youku":{},
 42                 "Iqiyi":{},
 43                 '汽车之家':{},
 44                 '新东方':{},
 45                 "QQ":{},
 46             }
 47         },
 48     },
 49     '上海':{
 50         "浦东":{
 51             "陆家嘴":{
 52                 "CICC":{},
 53                 "高盛":{},
 54                 "摩根":{},
 55             },
 56             "外滩":{},
 57         },
 58         "闵行":{},
 59         "静安":{},
 60     },
 61     '山东':{
 62         "济南":{},
 63         "德州":{
 64             "乐陵":{
 65                 "丁坞镇":{},
 66                 "城区":{},
 67             },
 68             "平原":{},
 69         },
 70         "青岛":{},
 71     },
 72 }
 73 back_flag = False
 74 exit_flag = False
 75 while not back_flag and not exit_flag:
 76 
 77 
 78     for key in menu:
 79         print(key)
 80     choice = input("1>>>:").strip()
 81     if choice == "q":
 82         exit_flag = True
 83     if choice in menu:
 84         while not back_flag and not exit_flag:     #让程序停在第二层
 85             for key2 in menu[choice]:
 86                 print(key2)
 87             choice2 = input("2>>>:").strip()
 88             if choice2 == "b":
 89                 back_flag = True
 90             if choice2 == "q":
 91                 exit_flag = True
 92             if choice2 in menu[choice]:
 93                 while not back_flag and not exit_flag:     #让程序停在第三层
 94                     for key3 in menu[choice][choice2]:
 95                         print(key3)
 96                     choice3 = input("3>>>:").strip()
 97                     if choice3 == "b":
 98                         back_flag = True
 99                     if choice3 == "q":
100                         exit_flag = True
101                     if choice3 in menu[choice][choice2]:
102                         while not back_flag and not exit_flag:
103                             for key4 in menu[choice][choice2][choice3]:
104                                 print(key4)
105                             choice4 = input("4>>>:").strip()
106                             print("last level")
107                             if choice4 == "b":
108                                 back_flag = True
109                             if choice4 == "q":
110                                 exit_flag = True
111                                 break
112                         else:
113                             back_flag = False
114                 else:
115                     back_flag = False
116         else:
117             back_flag = False
118             
119 #升级版三层菜单
120 menu = {
121     '北京':{
122         '朝阳':{
123             '国贸':{
124                 'CICC':{},
125                 'HP':{},
126                 '渣打银行':{},
127                 'CCTV':{},
128             },
129             '望京':{
130                 '陌陌':{},
131                 '奔驰':{},
132                 '360':{},
133             },
134             '三里屯':{
135                 '优衣库':{},
136                 'apple':{},
137             },
138         },
139         '昌平':{
140             '沙河':{
141                 '老男孩':{},
142                 '阿泰包子':{},
143             },
144             '天通苑':{
145                 '链家':{},
146                 '我爱我家':{},
147             },
148             '回龙观':{},
149         },
150         '海淀':{
151             '五道口':{
152                 '谷歌':{},
153                 '网易':{},
154                 'Sohu':{},
155                 'Sogo':{},
156                 '快手':{},
157             },
158             '中关村':{
159                 "youku":{},
160                 "Iqiyi":{},
161                 '汽车之家':{},
162                 '新东方':{},
163                 "QQ":{},
164             }
165         },
166     },
167     '上海':{
168         "浦东":{
169             "陆家嘴":{
170                 "CICC":{},
171                 "高盛":{},
172                 "摩根":{},
173             },
174             "外滩":{},
175         },
176         "闵行":{},
177         "静安":{},
178     },
179     '山东':{
180         "济南":{},
181         "德州":{
182             "乐陵":{
183                 "丁坞镇":{},
184                 "城区":{},
185             },
186             "平原":{},
187         },
188         "青岛":{},
189     },
190 }
191 
192 current_layer = menu    #实现动态循环
193 #parent_layer = menu
194 parent_layer = []       #保存所有父级,最后一个元素永远是父级
195 while True:
196     for key in current_layer:
197         print(key)
198     choice = input(">>>:").strip()
199     if len(choice) == 0:continue
200     if choice in current_layer:
201         parent_layer.append(current_layer)      #在进入下一级前,把当前层追加到列表中
202         #下一次pop,当用户选择b的时候,就可以直接取列表的最后一个一直出来
203         current_layer = current_layer[choice]   #改变子层
204     elif choice == "b":
205         if parent_layer:    #[]
206             current_layer = parent_layer.pop()  #取出列表的最后一个值
207     else:
208         print("无此项的")

 

#超low的三层菜单menu = {    '北京':{        '朝阳':{            '国贸':{                'CICC':{},                'HP':{},                '渣打银行':{},                'CCTV':{},            },            '望京':{                '陌陌':{},                '奔驰':{},                '360':{},            },            '三里屯':{                '优衣库':{},                'apple':{},            },        },        '昌平':{            '沙河':{                '老男孩':{},                '阿泰包子':{},            },            '天通苑':{                '链家':{},                '我爱我家':{},            },            '回龙观':{},        },        '海淀':{            '五道口':{                '谷歌':{},                '网易':{},                'Sohu':{},                'Sogo':{},                '快手':{},            },            '中关村':{                "youku":{},                "Iqiyi":{},                '汽车之家':{},                '新东方':{},                "QQ":{},            }        },    },    '上海':{        "浦东":{            "陆家嘴":{                "CICC":{},                "高盛":{},                "摩根":{},            },            "外滩":{},        },        "闵行":{},        "静安":{},    },    '山东':{        "济南":{},        "德州":{            "乐陵":{                "丁坞镇":{},                "城区":{},            },            "平原":{},        },        "青岛":{},    },}back_flag = Falseexit_flag = Falsewhile not back_flag and not exit_flag:

    for key in menu:        print(key)    choice = input("1>>>:").strip()    if choice == "q":        exit_flag = True    if choice in menu:        while not back_flag and not exit_flag:     #让程序停在第二层            for key2 in menu[choice]:                print(key2)            choice2 = input("2>>>:").strip()            if choice2 == "b":                back_flag = True            if choice2 == "q":                exit_flag = True            if choice2 in menu[choice]:                while not back_flag and not exit_flag:     #让程序停在第三层                    for key3 in menu[choice][choice2]:                        print(key3)                    choice3 = input("3>>>:").strip()                    if choice3 == "b":                        back_flag = True                    if choice3 == "q":                        exit_flag = True                    if choice3 in menu[choice][choice2]:                        while not back_flag and not exit_flag:                            for key4 in menu[choice][choice2][choice3]:                                print(key4)                            choice4 = input("4>>>:").strip()                            print("last level")                            if choice4 == "b":                                back_flag = True                            if choice4 == "q":                                exit_flag = True                                break                        else:                            back_flag = False                else:                    back_flag = False        else:            back_flag = False            #升级版三层菜单menu = {    '北京':{        '朝阳':{            '国贸':{                'CICC':{},                'HP':{},                '渣打银行':{},                'CCTV':{},            },            '望京':{                '陌陌':{},                '奔驰':{},                '360':{},            },            '三里屯':{                '优衣库':{},                'apple':{},            },        },        '昌平':{            '沙河':{                '老男孩':{},                '阿泰包子':{},            },            '天通苑':{                '链家':{},                '我爱我家':{},            },            '回龙观':{},        },        '海淀':{            '五道口':{                '谷歌':{},                '网易':{},                'Sohu':{},                'Sogo':{},                '快手':{},            },            '中关村':{                "youku":{},                "Iqiyi":{},                '汽车之家':{},                '新东方':{},                "QQ":{},            }        },    },    '上海':{        "浦东":{            "陆家嘴":{                "CICC":{},                "高盛":{},                "摩根":{},            },            "外滩":{},        },        "闵行":{},        "静安":{},    },    '山东':{        "济南":{},        "德州":{            "乐陵":{                "丁坞镇":{},                "城区":{},            },            "平原":{},        },        "青岛":{},    },}
current_layer = menu    #实现动态循环#parent_layer = menuparent_layer = []       #保存所有父级,最后一个元素永远是父级while True:    for key in current_layer:        print(key)    choice = input(">>>:").strip()    if len(choice) == 0:continue    if choice in current_layer:        parent_layer.append(current_layer)      #在进入下一级前,把当前层追加到列表中        #下一次pop,当用户选择b的时候,就可以直接取列表的最后一个一直出来        current_layer = current_layer[choice]   #改变子层    elif choice == "b":        if parent_layer:    #[]            current_layer = parent_layer.pop()  #取出列表的最后一个值    else:        print("无此项的")

推荐阅读