zoukankan      html  css  js  c++  java
  • dict的操作和三级菜单

    dict的基本操作

    # Author:nadech
    
    
    info = {
        "stu001":"sjp",
        "stu002":"cxx",
        "stu003":"lsw"
    }
    
    print(info)
    #########查#######
    #确定有这个值才用这种方法
    print(info["stu001"])
    #安全的获取方法
    print(info.get("stu005"))
    #判断是否存在
    print("stu002" in info)#在python2中与info.has_key("stu002")一样
    
    
    #########增#######
    info["stu004"] ="dyq"
    print(info)
    #########改#######
    info["stu001"] ="sjpppp"
    print(info)
    ##########删######
    #info.popitem()这个是随机删除
    info.pop("stu001")
    print(info)
    del info["stu002"]
    print(info)
    
    av_dicionary = {
        "europe":{"1":["a","b","c"],
                   2:["d","e","f"]
                  },
        "america":{3:["g","h","i"],
                   4:["j","k","l"]
                   },
        "tokyo":{6:["m","n","o"],
                  7:["p","q","r"]
                 }
    }
    #
    print(av_dicionary["tokyo"][6][0])
    print((av_dicionary.get("tokyo")).get(6))
    
    
    #
    av_dicionary["tokyo"][6][0] = "mmmmmmm"
    print(av_dicionary)
    #
    av_dicionary.setdefault("tokyo","www.baidu.com")
    print(av_dicionary)
    av_dicionary.setdefault("taiwan",{"wwww.baidu.com":["hahahah","henhaoyong"]})
    print(av_dicionary)
    
    info1 = {"s01":"nadech","s02":"aguilera","s03":"lsw"}
    print(info1)
    info2 = {1:2,"s03":"lswwww",3:4}
    print(info2)
    info1.update(info2)###跟新字典,存在的key值就将value更新,不存在的就加进去
    print(info1)
    
    #将字典转化为列表
    print(info1.items())
    
    #初始化字典使用fromkeys,当有多层列表时,它只是这个数据的引用,当修改一个字典的数据时,其他的也都会修改
    c = dict.fromkeys([6,7,8],["a",{1:"nadech"},2])
    print(c)
    c[6][1][1] = "aguilera"
    print(c)
    
    
    ####字典的循环
    
    ###推荐使用这个,因为更高效
    for i in  info1:
        print(i,info1[i])
    
    for i,v in info1.items():
        print(i,v)

    三级菜单

    # Author:nadech
    
    data = {
        "江苏":{
            "南京":{
                "栖霞":["南邮仙林校区","南师范仙林校区","南财仙林校区"],
                "鼓楼":["紫峰大厦","新街口","湖南路"]
            },
            "苏州":{},
            "无锡":{}
        },
        "河南":{},
        "云南":{}
    }
    print(data["江苏"]["南京"]["栖霞"])
    
    exit_flag = False
    while not exit_flag:
        for i in data:
            print(i)
        #判断输入是否在所存在的省份中
        choice1 = input(">>>请选择省份:")
    
    
        if choice1 in data:
    
            for j in data[choice1]:
                print(j)
            #判断输入是否在所存在的城市中
            choice2 = input(">>>请选择城市:")
            if choice2 in data[choice1]:
                for k in data[choice1][choice2]:
                    print(k)
                #判断输入的行政区是否存在
                choice3 = input(">>>请选择行政区:")
                if choice3 in data[choice1][choice2]:
                    print(data[choice1][choice2][choice3])
                else:
                    print("您的输入有误!")
            else:
                print("您的输入有误!")
    
        else:
            print("您的输入有误!")
  • 相关阅读:
    Vue(小案例_vue+axios仿手机app)_go实现退回上一个路由
    nyoj 635 Oh, my goddess
    nyoj 587 blockhouses
    nyoj 483 Nightmare
    nyoj 592 spiral grid
    nyoj 927 The partial sum problem
    nyoj 523 亡命逃窜
    nyoj 929 密码宝盒
    nyoj 999 师傅又被妖怪抓走了
    nyoj 293 Sticks
  • 原文地址:https://www.cnblogs.com/nadech/p/7666856.html
Copyright © 2011-2022 走看看