python基础之字典dict
Posted meifei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python基础之字典dict相关的知识,希望对你有一定的参考价值。
不可变数据类型:tuple、bool、int、str --可哈希类型
可变数据类型:list、dict、set --不可哈希类型
dict-key 必须是不可变数据类型,可哈希
dict-value 任意数据类型
dict 优点:二分查找、存储大量的关系型数据
特点:无序的-没有索引,只能按照键值对查找
增:dict[key] = value dict.setdefault(key) dict.setdefault(key ,value)
删:dict.pop(key) dict.pop(key,returns) dict.popitem() dict.clear() del dict[key] del dict
改:dict[key] = value dict1.update(dict2)
查:dict[key] dict.get(key) dict.get(key,returns)
操作: for k in dict: for k in dict.keys: for k,v in dict.items
统计:len(tu1)
1,新增:
dict[‘key‘] = value --原dict无key值时,进行新增,有key值时,进行修改
ey:
dict1 = ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ dict1[‘key1‘] = ‘V1‘ print(dict1)
result:‘key1‘: ‘V1‘, ‘key2‘: ‘val2‘, ‘key3‘: ‘val3‘
ey:
dict1 = ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ dict1[‘key5‘] = ‘V1‘ print(dict1)
result:‘key1‘: ‘val1‘, ‘key2‘: ‘val2‘, ‘key3‘: ‘val3‘, ‘key5‘: ‘V1‘
dict.setdefault(key)--默认value为None
dict.setdefault(key,value) --原dict无key值时,进行新增,有key值时,不变
ey:
dict1 = ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ dict1.setdefault(‘key1‘) print(dict1)
result:‘key1‘: ‘val1‘, ‘key2‘: ‘val2‘, ‘key3‘: ‘val3‘
ey:
dict1 = ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ dict1.setdefault(‘key5‘) print(dict1)
result:‘key1‘: ‘val1‘, ‘key2‘: ‘val2‘, ‘key3‘: ‘val3‘, ‘key5‘: None
ey:
dict1 = ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ dict1.setdefault(‘key5‘,‘V5‘) print(dict1)
result:‘key1‘: ‘val1‘, ‘key2‘: ‘val2‘, ‘key3‘: ‘val3‘, ‘key5‘: ‘V5‘
2,删除:
dict.pop(key)--返回删除的值,如果dict中无key值-报错
dict.pop(key,返回值)--可设置返回值,存在key则删除,不存在则返回后面加的返回值
ey:
dict1 = ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ dict1.pop(‘key1‘) print(dict1)
result:‘key2‘: ‘val2‘, ‘key3‘: ‘val3‘
ey:
dict1 = ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ print(dict1.pop(‘key11‘,‘不存在key11‘))
result:不存在key11
ey:
dict1 = ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ dict1.pop(‘key1‘,‘不存在key1‘) print(dict1)
result:‘key2‘: ‘val2‘, ‘key3‘: ‘val3‘
dict.popitem()--随机删除一个键值对,3.6版本按顺序删除,从后面开始删除
ey:
dict1 = ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ print(dict1.popitem()) print(dict1)
result:
(‘key3‘, ‘val3‘)
‘key1‘: ‘val1‘, ‘key2‘: ‘val2‘
dict.clear()--清空字典
ey:
dict1 = ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ print(dict1.clear()) print(dict1)
result:
None
del dict/dict[key]
ey:
dict1 = ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ del dict1[‘key2‘] print(dict1)
result:‘key1‘: ‘val1‘, ‘key3‘: ‘val3‘
ey:
dict1 = ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ del dict1 print(dict1)
result:NameError: name ‘dict1‘ is not defined
3,修改:
dict[key]=value --存在key时进行修改
dict1.update(dict2)--更新、覆盖添加
ey:
dict1 = ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ dict2 = ‘k‘:‘v‘,‘key1‘:‘va1‘ dict1.update(dict2) print(dict1)
result:‘key1‘: ‘va1‘, ‘key2‘: ‘val2‘, ‘key3‘: ‘val3‘, ‘k‘: ‘v‘
4,查询:
dict[key]
ey:
dict1 = ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ print(dict1[‘key1‘])
result:val1
dict.get(key,返回值)
ey:
dict1 = ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ print(dict1.get(‘key1‘))
result:val1
ey:
dict1 = ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ print(dict1.get(‘key11‘))
result:None
ey:
dict1 = ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ print(dict1.get(‘key11‘,‘不存在key‘))
result:不存在key
5,计算长度
dict1= ‘key1‘:‘val1‘,‘key2‘:‘val2‘,‘key3‘:‘val3‘ print(len(dict1))
result:3
以上是关于python基础之字典dict的主要内容,如果未能解决你的问题,请参考以下文章