本文作者:林箖霖😉
本文原创,未经允许,禁止转载!
7. Python中的字典
- 字典中的所有元素都是一个key-value对
- 字典与列表一样是一个可变序列(可进行增删改,但key为不可变对象)
- 以键值对的方式存储数据,字典是一个无序序列(存入的key通过hash变换)
- 字典是根据key查找value所在的位置
- 字典中不出现重复的key,但value可以重复,若在一次定义中出现两个相同的key,则最后出现的一个key类似于更新了前面相同的key的值
- 字典根据需要动态地伸缩,故其会浪费较大的内存,是一种空间换时间的数据结构
- 字典是元素可变、value可重复、key不可重复、元素无序排列的数据结构(可以进行增、删、改)
7.1 字典的创建
- 使用花括号 { }
# 使用花括号
# {key:value,key:value}
score = {'张三': 100, '李四': 90, '王五': 80, '李六': '九十', '李六': '九十'}
score2 = {} # 空字典
print(score, type(score)) # {'张三': 100, '李四': 90, '王五': 80, '李六': '九十'} <class 'dict'>
- 使用内置函数dict()
# 使用dict()函数
# dict(key=value)
people = dict(name='zhang', age=100, address='beijing')
print(people, type(people)) # {'name': 'zhang', 'age': 100, 'address': 'beijing'} <class 'dict'>
-
使用字典生成式
- 内置函数zip ( )
# 内置函数zip() Fruits = ['apple', 'orange', 'banana'] Numbers = [100, 200, 101] new_dict = zip(Fruits, Numbers) print(list(new_dict)) # 要转化为列表才能输出,因为zip()返回的是“由这些元组组成的列表”
- for…in…
# 字典生成式 Fruits = ['apple', 'orange', 'banana'] Numbers = [100, 200, 101] A = {Fruits: Numbers for Fruits, Numbers in zip(Fruits, Numbers)} print(A) # 如果两个字典中的元素不相等,则以短的字典为主 Tools = ['rule', 'desk', 'pen'] Prices = [100, 10, 20, 30, 102] B = {Tools: Prices for Tools, Prices in zip(Tools, Prices)} print(B) # {'rule': 100, 'desk': 10, 'pen': 20}
7.2 字典元素的获取
- 使用[ ]
# 使用[]
score = {'张三': 100, '李四': 90, '王五': 80, '李六': '九十'}
print(score['张三']) # 通过key值查询相应的value值 100
# print(score['zhang']) # 查找的key值不存在,抛出 KeyError: 'zhang'
- 使用get( )
# 使用get(),get(key,self)
print(score.get('李四')) # 通过key值查询相应的value值 90
print(score.get('zhang', 99)) # 查找相应的键值不存在,返回默认值99
print(score.get('zhang')) # 查找相应的键值不存在,返回None
7.3 字典元素的判断
- key的判断
# in / not in
score = {'张三': 100, '李四': 90, '王五': 80, '李六': '九十'}
print('zhang' in score) # False
print('张三' not in score) # False
7.4 字典元素的增删改
- 字典元素的增加
# 字典元素的添加
score = {'张三': 100, '李四': 90, '王五': 80, '李六': '九十'}
score['成七'] = 88
print(score) # {'张三': 100, '李四': 90, '王五': 80, '李六': '九十', '成七': 88}
-
字典元素的删除
- del
# 字典元素的删除 score = {'张三': 100, '李四': 90, '王五': 80, '李六': '九十'} del score['张三'] # 通过指定key删除键值对 print(score) # {'李四': 90, '王五': 80, '李六': '九十'}
- clear ( )
# 清空字典元素 score = {'张三': 100, '李四': 90, '王五': 80, '李六': '九十'} score.clear() print(score) # {}
-
字典元素的修改
# 字典元素的修改
score = {'张三': 100, '李四': 90, '王五': 80, '李六': '九十'}
score['成七'] = 400
print(score) # {'张三': 100, '李四': 90, '王五': 80, '李六': '九十', '成七': 400}
7.5 字典视图的获取
-
key ( )
# 获取字典中的所有key score = {'张三': 100, '李四': 90, '王五': 80, '李六': 100} print(score.keys()) # dict_keys(['张三', '李四', '王五', '李六']) print(type(score.keys())) # <class 'dict_keys'> # 将key转化成列表 print(list(score.keys())) # ['张三', '李四', '王五', '李六']
-
value ( )
# 获取字典中的所有value score = {'张三': 100, '李四': 90, '王五': 80, '李六': '九十'} print(score.values()) # dict_values([100, 90, 80, '九十']) print(type(score.values())) # <class 'dict_values'> # 将value转化成列表 print(list(score.values())) # [100, 90, 80, '九十']
-
items ( )
# 获取字典中的所有键值对 score = {'张三': 100, '李四': 90, '王五': 80, '李六': 100} print(score.items()) # dict_items([('张三', 100), ('李四', 90), ('王五', 80), ('李六', 100)]) print(type(score.items())) # <class 'dict_items'> # 将键值对转化为列表 print(list(score.items())) # [('张三', 100), ('李四', 90), ('王五', 80), ('李六', 100)]
7.6 字典元素的遍历
# 字典元素的遍历
score = {'张三': 100, '李四': 90, '王五': 80, '李六': 100}
for i in score:
print(i) # 获取key
print(score[i]) # 获取values
print(score.get(i)) # 获取values