什么是dict
dict
字典,存放 键值对-(key:value)
键不能重复,但是值可以重复,设计Key时尽量使用唯一且不可变的值
如果键重复,则该键值对的值以最后一个值为准
因为底层实现是哈希表,因此无下标
创建
dict1 = dict()
dict2 = {'key1':'value1','key2':'value2','key3':'value3'}
dict3 = {'key1':'value1','key1':'value2','key1':'value3'}
print(dict3)
===============================
{'key1': 'value3'}
访问
基本思路:通过 key
获取 value
-
一般的
如无指定被查找的键,则抛出一个异常
dict1 = {'key1':'value1','key2':'value2'} print(dict1['key1']) print(dict1['key0']) =========================== value1 KeyError: 'key0'
-
内置的
-
.get()
根据key
获取value
,可以规定当key
不存在时返回的默认值dict1.get( key , [ default ] )
dict1 = {'key1':'value1','key2':'value2'} print(dict1.get('key1')) print(dict1.get('key0')) print(dict1.get('key0','Not Value')) ============================== value1 None Not Value
-
.keys()
与.values()
取出字典里的所有键或所有值通过强制类型转换可以将输出的内容变成列表 Eg. list(dict1.keys())
dict1 = {'key1':'value1','key2':'value2'} print(dict1.keys()) print(dict1.values()) ============================= dict_keys(['key1', 'key2']) dict_values(['value1', 'value2'])
-
.items()
将所有键值对放入列表,每个键值对以元组的形式作为列表的一项利用for循环可以取出所有key或所有value
dict1 = {'key1':'value1','key2':'value2'} print(dict1.items()) =============================== dict_items([('key1', 'value1'), ('key2', 'value2')])
dict1 = {'key1':'value1','key2':'value2'} for key ,value in dict1.items(): # 通过.items()可以同时取到keys和values print(key,end='\t') print(value) ============================= key1 value1 key2 value2
直接将
dict
塞入循环仅能取到keys
dict1 = {'key1':'value1','key2':'value2'} for e in dict1: # 仅能取到keys print(e) ============================= key1 key2
-
增删改查
-
不支持
查询
,但支持取值
-
增加与修改的方法为
dict[key] = value
当无此
key
时,执行增加,当有此key
时执行修改dict1 = {'key1':'value1','key2':'value2'} dict1['key0'] = 'value0' dict1['key1'] = '***' print(dict1) =============================== {'key1': '***', 'key2': 'value2', 'key0': 'value0'}
-
删除的方法有
.pop()
.popitem()
.clear()
del()
.pop()
删除指定键值对,并将被删除的键值对的值返回.popitem()
随机删除一个键值对(从后向前),并将被删除的键值对以元组的形式返回.clear()
清空字典,但保留内存空间del()
删除指定键值对或清空字典且回收内存
dict1 = {'key1':'value1','key2':'value2','key3':'value3','key4':'value4'} print(dict1.pop('key1')) # 删除指定key1的键值对 print(dict1) print(dict1.popitem()) # 从后向前随机删除一个键值对 print(dict1) del dict1['key2'] # 删除指定key2的键值对 print(dict1) dict1.clear() # 清空dict1的内容 print(dict1) del dict1 # 回收dict1的内存空间 print(dict1) ============================== value1 {'key2': 'value2', 'key3': 'value3', 'key4': 'value4'} ('key4', 'value4') {'key2': 'value2', 'key3': 'value3'} {'key3': 'value3'} {} NameError: name 'dict1' is not defined
其他方法
-
.fromkeys()
借助可迭代对象
来创建一个新的字典list1 = ['key1','key2','key3'] dict1 = dict.fromkeys(list1,100) print(dict1) =============================== {'key1': 100, 'key2': 100, 'key3': 100}
转换
-
字典转列表 仅能将
keys
放入列表中dict1 = {1:2,3:4,5:6,7:8} list1 = list(dict1) print(list1) ======================== [1, 3, 5, 7]
-
列表转字典 仅能将如下格式的列表转换为字典
list1 = [(1,2),(3,4),(5,6)] 列表中套元组,且元组仅有一对元素
list1 = [(1,2),(3,4),(5,6)] dict1 = dict(list1) print(dict1) ========================= {1: 2, 3: 4, 5: 6}
支持的符号
in
检查key是否在dict中is
检查地址是否相同==
检查内容是否相同
不支持的符号
- 不支持
+
- 不支持
-
- 不支持
*