python字典dict 发表于 2019-10-22 | 更新于 2023-01-11 python字典dict练习123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990# 字典相当于js中的JSONa = {}print(a, type(a))a = dict(a=1, b=2, c=3)print(a, type(a))# 结构: {key: value}a = {'a': 1, 'b': '2', 'c': 3}print(a)# 根据key获取value, 不可以只用不存在的keya = {'a': 1, 'b': '2', 'c': 3}print(a['a'])# 字典转换 双值序列、子序列a = [('a', 1), ('b', 2)]print(dict(a))# 获取健值对a = {'a': 1, 'b': 2}print(len(a))# in: 字典中是否有健; not ina = {'a': 1, 'b': 2}print('c' in a, 'a' in a)# 不知道是否存在key,可以通过get获取a = {'a': 1}print(a.get('b', '默认值'))# 修改或者添加字典a = {'a': 1}a['b'] = 3print(a)# setDefault,有不做行为,没有修改a = {'a': 1}a.setdefault('a', 2)a.setdefault('b', 2)print(a)# update 合并字典 a修改、b不修改a = {'a': 1, 'b': 2}b = {'b': 1, 'c': 2}a.update(b)print(a, b)# 删除健值对a = {'a': 1, 'b': 2}del a['a']print(a)# popitem 删除并返回最后一个,返回元组,空字典删除会报错a = {'b': 1, 'a': 2}print(a.popitem())print(a)# pop根据key删除健值对a = {'b': 1, 'a': 2}a.pop('a')print(a)# pop指定默认值删除不报错,返回默认值a = {'a': 1}print(a.pop('b', 2)) # 2print(a)# 清空 cleara = {'a': 1}a.clear()print(a)# 浅拷贝a = {'a': 1}b = a.copy()print(a is b, a == b)# 遍历字典# keys 返回所有的keya = {'a': 1, 'b': 2, 'c': 3}for key in a.keys(): print(key, a[key])# values 返回所有的valuefor key in a.values(): print(key)# items 返回所有的双值子序列for item in a.items(): print(item)