Python——字典應用實例


三級列表實現

data={
    '北京':{
        '昌平':{
            '沙河':['oldboy','test'],
            '天通苑':['鏈家地產','我愛我家'],
        },
        '朝陽':{
            '望京':['奔馳','陌陌'],
            '國貿': ['CICC', 'HP'],
            '東直門': ['Advent', '飛信'],
        },
        '海淀':{
        },

    },
    '山東':{
        "德州": {},
        "青島": {},
        "濟南": {},
    },
    '廣東':{
        "東莞": {},
        "常熟": {},
        "佛山": {},
    },
}
exit_flag=False
while not exit_flag:
    #打印第一層
    for i in data:
        print(i)
    choice=input("選擇進入1>>:")
    if choice in data:
        while not exit_flag:
            #打印第二層
            for i2 in data[choice]:
                print("\t",i2)
            choice2=input("選擇進入2>>:")
            if choice2 in data[choice]:
                while not exit_flag:
                    #打印第三層
                    for i3 in data[choice][choice2]:
                        print('\t\t',i3)
                    choice3=input("選擇進入3>>:")
                    if choice3 in data[choice][choice2]:
                        #打印第四層
                        for i4 in data[choice][choice2][choice3]:
                            print('\t\t\t',i4)
                        choice4=input('最后一層,按b返回>>:')
                        if choice4=='b':
                            pass
                        elif choice4=='q':
                            exit_flag=True
                    if choice3 == 'b':
                        break
                    elif choice3 == 'q':
                        exit_flag = True
            if choice2 == 'b':
                break
            elif choice2 == 'q':
                exit_flag = True
    if choice == 'q':
        exit_flag = True

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM