#Author:ersa
'''
key-value 键值对
字典是无序的,不需要下标,有key
字典的查找、修改、添加、判断、删除
'''
info = {
'stu1101': "TengLan Wu",
'stu1102': "LongZe Luola",
'stu1103': "XiaoZe Maliya",
}
#输出
print("输出所有的key".center(50,'='))
print(info.keys())
print("=".center(70,'='))
print("输出所有的 值".center(50,'='))
print(info.values())
print("=".center(70,'='))
#循环输出键-值
print("输出键-值".center(50,'='))
for i in info:
print(i,info[i])
print("=".center(70,'='))
#设置默认值:如果有key-value 则返回,没有则创建
print("取key-value值,没有则创建".center(50,'='))
info.setdefault("stu1106","Alex")
print(info)
print("=".center(70,'='))
#查找
print(info)
print("查找".center(50,'='))
print(info["stu1101"])
#安全查找--->查找的值不存在时不会报错
print("安全查找".center(50,'='))
print(info.get('stu1104'))
#判断
print("判断key是否存在".center(50,'='))
print('stu1104' in info)
#修改
print("修改".center(50,'='))
print(info)
info["stu1101"] = "武藤兰"
#添加
print("添加".center(50,'='))
info["stu1104"] = "苍老师"
print(info["stu1104"])
#删除 Python--->通用删除del
print("删除".center(50,'='))
#del info["stu1101"]
print(info)
#字典删除
print("字典删除".center(50,'='))
#info.pop("stu1101")
print(info)
#随机删除
print("随机删除".center(50,'='))
#info.popitem()
print(info)
#字典转列表
print("字典转列表".center(50,'='))
print(info.items())
print("=".center(70,'='))
#update 更新字典
b = {
'stu1101':"alex",
1:3,
2:5
}
info.update(b)
print("合并字典,有重合key,则更新".center(50,'='))
print(info)
print("=".center(70,'='))
#初始化新的字典
print("初始化新的字典".center(50,'='))
c = dict.fromkeys([6,7,8],'test')
print(c)
print("=".center(70,'='))