【python--字典】 字典的增删改查
# (1)字典的增
# 通过键值对直接增加
# 有键值则覆盖,如果没有键值则添加
dic = {'ni':1,'woazia':2}
dic['hello'] = 6 # 没有'hello'这个键,就增加键值对
print(dic) # {'ni': 1, 'woazia': 2, 'hello': 6}
dic['ni'] = 65 # 有'ni'这个键,就成了字典的改值
print(dic) # {'ni': 65, 'woazia': 2, 'hello': 6}
# setdefault
#有键值不做任何改变,没有键值才添加, 无论有没有简直,返回值为待添加的后面的键值
dic = {'ni':1,'woazia':2}
dic.setdefault('woshi',1) # 没有'woshi'此键,则添加
print(dic) # {'ni': 1, 'woazia': 2, 'woshi': 1}
dic.setdefault('ni','kaishi') # 有此键则不变 不发生改变
print(dic) # {'ni': 1, 'woazia': 2, 'woshi': 1}
# 它有返回值
dic = {'ni':1,'woazia':2}
re = dic.setdefault('ni')
print(re) # 返回 value --1
# (2) 删
# pop 索引删除字典对应的元素 可设置返回值
dic = {'name':'xuanchenli','age':21}
re = dic.pop('name') # 删除key 返回对应的 value
print(dic)
# {'age': 21}
print(re) # 返回key 对应的 值
# xuanchenli
# popitem (删除默认最后一个)
dic = {'name':'xuanchenli','age':21}
re = dic.popitem()
print(dic)
# {'name': 'xuanchenli'}
print(re) # 返回 元组
# ('age', 21)
print(re,dic)
# ('age', 21) {'name': 'xuanchenli'}
# clear 清空字典
dic = {'name':'xuanchenli','age':21}
dic.clear()
print(dic)
# {}
#
# # del
# # 通过键删除键值对
dic = {'name':'xuanchenli','age':21}
del dic['name']
print(dic)
# {'age': 21}
# 删除整个字典
dic = {'name':'xuanchenli','age':21}
del dic
print(dic)
# (3)改
#通过键值对直接改
dic = {'name':'xuanchenli','age':21}
dic['name'] = 'lxc'
print(dic)
#{'name': 'lxc', 'age': 21}
#
# #update 修正
dic = {'name':'xuanchenli','age':21}
dic.update(sex = 'nan',hei = '175') # 映射关系 如果改的 键值对 不在字典里 则添加
print(dic)
# {'name': 'xuanchenli', 'age': 21, 'sex': 'nan', 'hei': '175'}
dic = {'name':'xuanchenli','age':21}
dic.update([(1,'a'),(3,'c')])
print(dic)
# {'name': 'xuanchenli', 'age': 21, 1: 'a', 3: 'c'}
dic = {'name':'xuanchenli','aoa': 21, 1: 'a', 3: 'c','age':21}
dic1 = {'name':'lxc','age':22}
dic.update(dic1)
print(dic)
# {'name': 'lxc', 'aoa': 21, 1: 'a', 3: 'c', 'age': 22}
# # (self: dict, __m: Mapping[_KT, _VT], kwargs: Dict[str, _VT]) -> None Mapping[_KT, _VT], 映射关系 Dict[str, _VT]
# # • (self: dict, __m: Iterable[Tuple[_KT, _VT]], kwargs: Dict[str, _VT]) -> None Iterable[Tuple[_KT, _VT]] 可迭代对象 元组组成 tuple Dict[str, _VT]
# # • (self: dict, kwargs: Dict[str, _VT]) -> None Dict[str, _VT]
# (4)查
# 通过键查找
dic = {'name':'xuanchenli','aoa': 21, 1: 'a', 3: 'c','age':21}
print(dic['name'])
# xuanchenli 打印出 对应 值
# get 查找key 传出对应的 value
dic = {'name':'xuanchenli','aoa': 21, 1: 'a', 3: 'c','age':21}
re = dic.get('name')
print(re) # 查找key 传出对应的 value
# xuanchenli
# keys 查找keys 所有的key
dic = {'name':'xuanchenli','aoa': 21, 1: 'a', 3: 'c','age':21}
print(dic.keys())
# dict_keys(['name', 'aoa', 1, 3, 'age'])
re = dic.keys()
for i in re: # 迭代所有 key
print(i)
# name
# aoa
# 1
# 3
# age
# values 查找字典里所有的 value
dic = {'name':'xuanchenli','aoa': 21, 1: 'a', 3: 'c','age':21}
print(dic.values())
#dict_values(['xuanchenli', 21, 'a', 'c', 21])
re = dic.values()
for i in re: # 迭代所有的 value
print(i)
# xuanchenli
# 21
# a
# c
# 21
# items
# 获取所有的键值对
dic = {'name':'xuanchenli','aoa': 21, 1: 'a', 3: 'c','age':21}
print(dic.items())
# dict_items([('name', 'xuanchenli'), ('aoa', 21), (1, 'a'), (3, 'c'), ('age', 21)])
re = dic.items()
for i in re:
print(i)
# ('name', 'xuanchenli')
# ('aoa', 21)
# (1, 'a')
# (3, 'c')
# ('age', 21)
# 拆包
for k,v in re:
print(k,v,sep='----')
# name----xuanchenli
# aoa----21
# 1----a
# 3----c
# age----21