字典基础

复制代码
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:woshinidaye
#列表、元组、字符串、字典、集合、文件、字符编码转码、内置函数
#列表、元组、字符串、字典、集合、文件、字符编码转码、内置函数

#如果一个对象,存在大量的可变参数,如果要把这些都保存下来,按理列表也能实现,但是取的时候比较麻烦,这个时候就可以考虑使用字典了!
#key-value的数据类型
'''
stu = {
    'stu1':'zhangsan',          #K-V是一一对应的
    'stu2':'lisi',
    'stu3':'laowang',
    'Abc':'1Abc',
}
print(stu)

#输出{'stu1': 'zhangsan', 'stu2': 'lisi', 'stu3': 'laowang', 'Abc': '1Abc'}
#字典是没得顺序的!!!!!所以没得下标,index
print(stu['stu1'])          #查
stu['stu1']='123456'        #修改
stu['stu4']='asan'          #增
#del stu['Abc']              #删除
#stu.pop('Abc')              #删除
#stu.popitem()                   #因为字典没有顺序,所以随便删除一个
print(stu)
#补充一下查找
print(stu['stu1'])      #如果查找的对象存在,则正常输出,否则会报错,所以用这个方法一定要保证数据存在
#print(stu['stu12345678'])
print(stu.get('stu123'),stu.get('stu1'))        #没有会返回NONE
print('stu1' in stu,'stu78909090' in stu)       #判断是否存在,等同于python2中 stu.has_key('stu1')

print(' parting-1'.rjust(40,'>'),''.ljust(40,'<'))
#字典支持多级嵌套,里面的KEY-value可以是列表、元组
#因为之前说的K-V是一一对应,stu1有多个变量就需要用到列表

teacher = {
    'boduo':[160,34,'D'],
    'mali':[157,36,'C'],
    'julia':[162,36,'F']
}
print(teacher)
print(teacher.values())     #把所有的value打印出来,不需要KEY
print(teacher.keys())       #把所有的key打印出来,不需要value
print(teacher['mali'][1])
print(teacher.get('mali')[0])
print(teacher.get('mali1231231231231'))
teacher.setdefault('cangjingkong',[156,36,'D'])      #setdefault  没有cangjingkong就创建一个,有的话,就返回相应的值
teacher.setdefault('julia',[100,32,'HHHHHHHH'])
print(teacher.items(),type(teacher.items()))

for i in teacher:
    print(i,teacher[i])   #建议使用这类方式,效率更高
for k,v in teacher.items():
    print(k,v)
print('ending'.center(100,'='))
'''

#三级菜单的作业
China ={
    'chongqing':{'tongliang':['yongjia','xinmin'],
                'yongchuan':['tuqiao','gonghe'],
                    },
    'sichuan':{'chengdu':['wuhou','jinjiang'],
               'mianyang':['mianzhu','santai']
               },
    'guizhou':{'guiyang':['duyun','longodngbao'],
               'liupanshui':['liuzhi','anshun']
               }
}
#print(China())
while True:
    for i in China:
        print(i)
    print('an q or Q 退出')
    print('press up or UP 退出')
    choice_1 = input('qing shu ru>>>>11>>>'.rjust(50,'='))
    if choice_1 in China:
        while True:
            for a in China[choice_1]:
                print('\t',a)
            print('an q or Q 退出')
            print('press up or UP 退出')
            choice_2 = input('\t\tqing shu ru>>22>>>'.rjust(50,'='))
            if choice_2 in China[choice_1]:
                while True:
                    for b in China[choice_1][choice_2]:
                        print('\t\t',b)
                    print('an q or Q 退出')
                    print('press up or UP 退出')
                    choice_3 = input('\t\t\tqing shu ru>>33>>>'.rjust(50,'='))
                    if choice_3 in China[choice_1][choice_2]:
                        print(choice_3)
                    elif choice_3 == 'q' or choice_3 =='Q':
                        print('byebye')
                        exit()
                    elif choice_3 == 'up' or choice_3 == 'UP':
                        break
                    else:
                        continue
            elif choice_2 == 'q' or choice_2 == 'Q':
                print('byebye')
                exit()
            elif choice_2 == 'up' or choice_2 == 'UP':
                break
            else:
                print('\tni de xuan zhe you wu')
    elif choice_1 == 'q' or choice_1 == 'Q':
        print('byebye')
        exit()
    elif choice_1 == 'up' or choice_1 == 'UP':
        break
    else:
        print('\t ni de xuan ze you wu')
复制代码

 

posted @   woshinidaye  阅读(44)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· Qt个人项目总结 —— MySQL数据库查询与断言
点击右上角即可分享
微信分享提示