python----字典
创建字典
# 创建空字典
dit1 = {}
dit2 = dict()
print(type(dit1),type(dit2)) # <class 'dict'> <class 'dict'>
# 创建字典并初始化值
d1 = {"a":1,"b":2,"c":3}
print(d1) # {'a': 1, 'b': 2, 'c': 3}
d2 = dict(a=1,b=2,c=3)
print(d2) # {'a': 1, 'b': 2, 'c': 3}
ls1 = ["a","b","c"]
ls2 = [1,2,3]
d3 = dict(zip(ls1,ls2))
print(d3) # {'a': 1, 'b': 2, 'c': 3}
注意:{键1:值1,键2:值2},字典的元素是由键值对组成
获取键-值对的数量
字典的键不可重复
dit = {'a': 1, 'b': 2, 'c': 3}
le = len(dit)
print(le) # 3
用键查找字典的值
dit = {'键1':'值1','键2':'值2','键3':'值3'}
print(dit['键1']) # 值1
print(dit['键2']) # 值2
print(dit['键3']) # 值3
修改字典元素
dit = {'键1':'值1','键2':'值2','键3':'值3'}
dit['键1'] = 666
dit['键2'] = 888
dit['键3'] = 999
# 若字典中没有这个键,则添加一个键值dui
dit['键4'] = "haha"
print(dit) # {'键1': 666, '键2': 888, '键3': 999, '键4': 'haha'}
注意:建议使用 dit[‘键4’] = "haha"添加键值对,不建议用fromkeys() 添加键值对。
del删除字典元素
dit = {'键1': 666, '键2': 888, '键3': 999, '键4': 'haha'}
del dit['键1'],dit['键2']
print(dit) # {'键3': 999, '键4': 'haha'}
# 删除整个字典
del dit
print(dit) # 报错:NameError: name 'dit' is not defined. Did you mean: 'dir'?
查看字典中是否存在某个键
dit = {'键1': 666, '键2': 888, '键3': 999, '键4': 'haha'}
a = '键1' in dit
print(a) # True
b = 'aa' in dit
print(b) # False
字典方法
(1)、fromkeys()
dic = {}.fromkeys("a","haha")
print(dic) # {'a': 'haha'}
lst = ["键1","键2","键3"]
dic1 = {}.fromkeys(lst,"666")
print(dic1) # {'键1': '666', '键2': '666', '键3': '666'}
# "键1","键2","键3",对应的值都都同时指向同一个内存地址
print(id(dic1['键1'])) # 1881642265200
print(id(dic1['键2'])) # 1881642265200
print(id(dic1['键3'])) # 1881642265200
print(dic1['键1'] is dic1['键2']) # True
(2)、update()
dit = {'键1':'值1','键2':'值2','键3':'值3'}
# 有该键就更新
d = dit.update({'键1': 666, '键2': 888, '键3': 999})
print(d) # None
print(dit) # {'键1': 666, '键2': 888, '键3': 999}
# 没该键就添加
dit.update({"键4":"值4"})
dit.update(键5="值5")
print(dit) # {'键1': 666, '键2': 888, '键3': 999, '键4': '值4', '键5': '值5'}
(3)、pop()
# 有键则返回键对应的值
dit = {'键1':'值1','键2':'值2','键3':'值3'}
d = dit.pop('键1',"字典中没有该键值对")
print(d) # 值1
print(dit) # {'键2': '值2', '键3': '值3'}
# 没有键则返回预设值
d1 = dit.pop('键5',"字典中没有该键值对")
print(d1) # 字典中没有该键值对
(4)、popitem()
dit = {'键1':'值1','键2':'值2','键3':'值3'}
d = dit.popitem()
print(d) # ('键3', '值3')
print(dit) # {'键1': '值1', '键2': '值2'}
(5)、clear()
dit = {"top1":"钟无艳","top2":"貂蝉","top3":"后羿"}
d = dit.clear()
print(d) # None
print(dit) # {}
(6)、get()
dit = {"top1":"钟无艳","top2":"貂蝉","top3":"后羿"}
# 当键存在时
d = dit.get("top1")
print(d) # 钟无艳
# 当键不存在时且没有预设值时
d1 = dit.get("top5")
print(d1) # None
# 当键不存在时且有预设值时
d2 = dit.get("top5",1)
print(d2) # 1
(7)、setdefault()
dit = {"top1":"钟无艳","top2":"貂蝉","top3":"后羿"}
# 当键存在时
d = dit.setdefault("top1")
print(d) # 钟无艳
# 当键不存在时且没有预设值时
d1 = dit.setdefault("top5")
print(d1) # None
print(dit) # {'top1': '钟无艳', 'top2': '貂蝉', 'top3': '后羿', 'top5': None}
# 当键不存在时且有预设值时
d2 = dit.setdefault("top6","没有该键值对")
print(d2) # None
print(dit) # {'top1': '钟无艳', 'top2': '貂蝉', 'top3': '后羿', 'top5': None, 'top6': '没有该键值对'}
(8)、keys()
dit = {"top1":"钟无艳","top2":"貂蝉","top3":"后羿"}
d = dit.keys()
print(d) # dict_keys(['top1', 'top2', 'top3'])
print(type(d)) # <class 'dict_keys'>
(9)、values()
dit = {"top1":"钟无艳","top2":"貂蝉","top3":"后羿"}
d = dit.values()
print(d) # dict_values(['钟无艳', '貂蝉', '后羿'])
print(type(d)) # <class 'dict_values'>
(10)、items()
dit = {"top1":"钟无艳","top2":"貂蝉","top3":"后羿"}
d = dit.items()
print(d) # dict_items([('top1', '钟无艳'), ('top2', '貂蝉'), ('top3', '后羿')])
print(type(d)) # <class 'dict_items'>
列表、元组与字典之间的转换
dit = {"top1":"钟无艳","top2":"貂蝉","top3":"后羿"}
# 字典转列表
ls = list(dit)
# 默认将键转换为列表
print(ls) # ['top1', 'top2', 'top3']
# 字典转元组
tp = tuple(dit)
# 默认将键转换为元组
print(tp) # ('top1', 'top2', 'top3')