字典

# dic ={"jj":"林俊杰","jay":"周杰伦","tz":"陶洁",1:"哈哈",(1,2,3):"胡辣汤"}
# print(dic)
# 字典的新增:

# dic = {}
# #徐峥:人在囧途
# dic["徐峥"] = "人在囧途" #直接用key往里面存数据即可
#
# dic["黄渤"] = "疯狂的石头"
#
# dic["王宝强"] = "天下无贼"
#
# dic["王宝强"] = "士兵突击"#如果key哟存在,那么会替换掉原来的value 属于修改
#
# dic.setdefault("黄秋生","头文字D")
# dic.setdefault("黄秋生","无间道")#如果存在了key,你会执行新增
#
# print(dic)

#删除
# dic = {"黄日华":"天龙八部","吕颂贤":"笑傲江湖","苏有朋":"倚天屠龙记","六小龄童":"西游记"}
# dic.pop("吕颂贤")# 制定key删除
# dic.popitem() #随机删除
#del dic["苏有朋"]#删除
#dic.clear() #清空字典

#print(dic)

#修改
# dic = {"刘能":"王小丽","赵四":"刘小光","王木生":"范伟","谢大脚":"于月仙","李大国":"小鬼"}
# dic["刘能"] = "王小利"
# dic2 = {"刘能":"大阳哥","赵四":"赵本上","王木生":"汪峰","谢大脚":"冯提莫","王大拿":"金老板"}
# dic.update
# print(dic)
#查询
#dic = {"刘能":"王小丽","赵四":"刘小光","王木生":"范伟","谢大脚":"于月仙","李大国":"小鬼"}
#1.最直观的,直接用key
#print(dic["周杰伦"])#当这个key不存在的时候会报错
#2. get方法
#print(dic.get("周杰伦","周杰伦不在这里"))#没有key,返回None
#3 setdefault()#.新曾先看看有没有key,如果有就过,如果没有,执行新增 2.根据key吧值返回
# dic = {}
# #dic["盖伦"] = "德玛西亚之力"
#
# value = dic.setdefault("菲欧娜","无双剑姬")#新增
# print(dic)
# value2 = dic.setdefault("盖伦","刘伟")#由于已经存在了key,suoyi新增不执行,直接查询结果
# print(value2)
# value3 = dic.setdefault("维恩","坑")
# print(value3)
# print(dic)

#都说 龙梅子
# dic = {"汪峰":"大陆音乐半壁江山","周杰伦":"亚洲音乐天王","罗志祥":"亚洲舞王"}
#
# print(dic.keys()) #dict_keys(['汪峰', '周杰伦', '罗志祥'])向列表但不是列表
# for key in dic.keys():
# print(key)# 拿到key
# print(dic[key])#拿到value
#
# print(dic.values())
# for value in dic.values():
# print(value)
#
# print(dic.items()) #拿到的是key和value
#
# for item in dic.items():
# print(item) #元组
# print(item[0]),item[1]
#
# for k, v in dic.items():#当需要遍历字典,在操作中涉及到key和value的shihou
# print(k)
# print(v)
#
# a = 10
# b = 20
# print(a)
# print(b)
#
# #前面的变量的个数和后面的解包的个数一致
# a,b = 10,20# 专业词称 解构,解包
# print(a)
# print(b)
#
#
# #字典本身是一个可迭代对象,可以直接进行for循环
# for el in dic:
# print(el)
# print(dic[el])


#
# wf = {
# "name": "汪峰",
# "age": 48,
# "成名曲": "怒放的生命",
# "wife": {
# "name": "章子怡",
# "age": 39,
# "工作": "演员"
# },
# "children":[
# {"num": "001", "name": "汪一", "hobby": "唱歌"},
# {"num": "002", "name": "汪二", "hobby": "演戏"} # wf['children'][1]['name']
# ]
# }
# print(wf["children"][1]["hobby"])
# print(wf["wife"]["工作"])
#
#
# wf["wife"]["age"] = wf["wife"]["age"]+10
# print(wf)


字典的增删改查
字典里的新增
dict[新key] = value
dict.setdefault()
删除
pop(key)
popitem()
clear()
del dict[key]
修改
dic[key] = 新值
update()
查询
用key直接查询dict[key]
get(key,如果key不存在返回的数据)
setdefault() 执行新增流程 查询结果
操作
keys() 获取所有键 这个返回的不是列表,只不过很像列表
values()获取所有的值
items()获取所有的键值,返回的是元组
解构,
直接把元组或者列表中的数据拿出来
a,b,c = (a1, b1, c1)必须一一对应
遍历字典
for key in dict:
dict[key]

for k,v in dict.items():
k,v
posted @ 2018-09-07 19:34  若无过客丶何来人生  阅读(188)  评论(0编辑  收藏  举报