字典
字典
定义
字典 -- dict
字典是无序的,可变的数据类型
字典:用于存储数据,存储大量数据,字典要比列表快,将数据和数据之间进行关联
定义一个字典:
dic = {"常鑫":10,"李文虎":80,"张宇":100}
字典中逗号分隔叫做一个元素"常鑫"(键):10(值) -- 键值对
dic = {"常鑫":10,"李文虎":80,"张宇":100,10:"大黑哥",True:123,(1,2,3):"大煎饼",[1,2]:"meet"}
print(dic)
字典的键:
可哈希的 --- 不可变的数据类型 --- 避免哈希冲突使用了 开放寻址法
不可哈希的 --- 可变的数据类型
要求唯一 --- 如果有重复的后边值的将前面的值覆盖
字典的值:
可以任意
字典的增,删,改,查
增:
dic = {"常鑫":10,"李文虎":80,"张宇":100}
dic["李文虎"] = 89
print(dic) # dic = {"常鑫":10,"李文虎":89,"张宇":100}
# 暴力添加
dic = {"常鑫":10,"李文虎":80,"张宇":100}
dic.setdefault("大黑哥",50)
print(dic) # {'常鑫': 10, '李文虎': 80, '张宇': 100, '大黑哥': 50}
# "大黑哥"这个键去字典查询,没有就添加,有就不执行添加.
删除:
dic = {"常鑫":10,"李文虎":80,"张宇":100}
del dic["常鑫"]
prin(dic) # {'李文虎': 80, '张宇': 100}
# 通过字典的键删除键值对
dic = {"常鑫":10,"李文虎":80,"张宇":100}
print(dic.pop("常鑫")) # 10
print(dic) # {'李文虎': 80, '张宇': 100}
# 通过字典的键进行删除键值对 返回值:返回的是删除的值
dic = {"常鑫":10,"李文虎":80,"张宇":100}
dic.clear()
print(dic) #{}
# 清空
改:
dic = {"常鑫":10,"李文虎":80,"张宇":100}
dic["常鑫"] = "dsb"
print(dic) # {'常鑫': 'dsb', '李文虎': 80, '张宇': 100}
# 字典中没有某个键值对的时候就是添加,当键在字典中存在的时候就是修改
# 有则改,无则加
dic = {"常鑫":10,"李文虎":80,"张宇":100}
dic.update({"常鑫":50,"张宇":30})
print(dic) # {'常鑫': 50, '李文虎': 80, '张宇': 30}
# update括号中的字典级别高于update前面的字典
查:
dic = {"常鑫":10,"李文虎":80,"张宇":100}
for i in dic:
print(i)
# 常鑫
# 李文虎
# 张宇
#循环获取的是字典中每个键值对的键
dic = {"常鑫":10,"李文虎":80,"张宇":100}
print(dic.get("李文虎")) # 80
# 通过键获取值,如果将不存在字典中返回None
print(dic["ck"]) # 报错
# 通过键获取值,如果将不存在字典中报错
其他操作
dic = {"1":3,"5":9,"李文虎":100}
for i in dic:
print(i,dic.get(i))
# 1 3
# 5 9
# 李文虎 100
# 获取字典中所有的键和值
dic = {"1":3,"5":9,"李文虎":100}
print(dic.keys()) # dict_keys(['1', '5', '李文虎'])
# 高仿列表 -- dict_keys(['1', '5', '李文虎']) 支持for循环 不支持索引
dic = {"1":3,"5":9,"李文虎":100}
for i in dic.keys():
print(i) # 1
# 5
# 李文虎
# 获取字典中所有的键
dic = {"1":3,"5":9,"李文虎":100}
lst = []
for i in dic:
lst.append(dic[i])
print(lst) #[3, 9, 100]
# 获取字典中所有的值
print(list(dic.values())) #[3, 9, 100]
# 获取字典中所有的值
dic = {"1":3,"5":9,"李文虎":100}
print(dic1.items())
# dict_items([('1', 3), ('5', 9), ('李文虎', 100)])
dic = {"1":3,"5":9,"李文虎":100}
for i in dic.items():
print(i[0],i[1]) # 1 3
# 5 9
# 李文虎 100
解构
a,b = 10,12
print(a)
print(b) # 10
# 12
a = 10
b = 20
a,b = b,a
print(a,b) # 20 10
a,b = [1,2]
print(a,b) # 1 2
a,b = (3,4)
print(a,b) # 3 4
a,_,b = (3,4,5)
print(a,b) # 3 5 等号后边的值和前面的变量名要一一对应
a = 10,12
print(a) # (10,12) 本质就是一个元组
a,b = "23"
print(a,b) # 2 3
a,b = {"1":"a","2":"b"}
print(a,b) # 1 2
dic = {"1":3,"5":9,"李文虎":100}
for i in dic.items():
print(i) # ('1', 3)
# ('5', 9)
# ('李文虎', 100)
dic = {"1":3,"5":9,"李文虎":100}
for k,v in dic.items():
print(k,v) # 1 3
# 5 9
# 李文虎 100
# 获取每个元素的键和值
字典的嵌套
dic = {1001:["周杰伦","林俊杰"],
1002:{"汪峰":{"前妻1":["大烧饼"],"前妻2":["熊大","熊二"],"国际章":"肉丝"}},
1003:{"陈冠希":["阿娇","阿萨","张柏芝"]},
1004:{"韦小宝":[{"教主夫人":["教主"]},{"阿珂":"刺客"},{"双儿":"天地会"}, {"爱心觉罗建宁":"公主"}, {"独臂神尼":{"韦小宝师父":{"兰陵王":"隐身"},"花木兰":[ "唧唧复唧唧","木兰是战士",{"爸爸":["花和尚"]}]}}, {"陈圆圆":"丈母娘"},{"穆建平":"小郡主"}]},
1005:{"常鑫":["大黑哥","大烧饼","吃大煎饼","肉夹馍","自行车","井盖",]}
}
找到"教主"
print(dic[1004]["韦小宝"][0]["教主夫人"][0])
找到"井盖"
print(dic[1005]["常鑫"][-1])
找到"熊二"
print(dic[1002]["汪峰"]["前妻2"][-1])
找到"隐身"
print(dic[1004]["韦小宝"][4]["独臂神尼"]["韦小宝师父"]["兰陵王"])
找到"花和尚"
print(dic[1004]["韦小宝"][4]["独臂神尼"]["花木兰"][2]["爸爸"][0])