码迷,mamicode.com
首页 > 编程语言 > 详细

python基础之字典dict

时间:2019-08-20 18:37:15      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:可变   ali   一个   返回值   color   efault   删除   数据类型   哈希类型   

不可变数据类型: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

标签:可变   ali   一个   返回值   color   efault   删除   数据类型   哈希类型   

原文地址:https://www.cnblogs.com/meifei/p/11384562.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!