python基础之字典dict

旧街凉风 提交于 2019-11-28 00:56:44
不可变数据类型: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为Nonedict.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:不存在key11ey:
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 defined3,修改: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:val1dict.get(key,返回值)ey:
dict1 = {'key1':'val1','key2':'val2','key3':'val3'}
print(dict1.get('key1'))
result:val1ey:
dict1 = {'key1':'val1','key2':'val2','key3':'val3'}
print(dict1.get('key11'))
result:Noneey:
dict1 = {'key1':'val1','key2':'val2','key3':'val3'}
print(dict1.get('key11','不存在key'))
result:不存在key5,计算长度
dict1= {'key1':'val1','key2':'val2','key3':'val3'}
print(len(dict1))
result:3
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!