字典:
字典是Python内置的数据结构之一,与列表一样是一个可变序列。以键值对的方式存储数据,字典是一个无序的序列。
与之相对的,列表是一个有序的序列。
字典的实现原理与查字典类似,查字典是先根据部首或拼音查找对应的页码,Python中的字典是根据key查找value所在的位置
字典的创建:
①最常用的方式:使用花括号
# 使用花括号
scores = {'张三':100, '李四':98, '王五':45}
# 使用内置函数dict()
student = dict(name = 'jack', age = 20)
# 空字典
d = {}
print(d) # {}
字典元素的获取
获取字典元素的方法:
① [ ], 例:scores[‘张三’]
②get()方法,例:scores.get(‘张三’)
[]取值与使用get()取值的区别:
[]如果字典中不存在指定的key,抛出异常KeyError;
get()方法取值,如果字典中不存在制定的key,并不会抛出KeyError而是返回None,可以通过参数设置默认的value,以便指定的key不存在时返回。
# 获取字典的元素
scores = {'张三': 100, '李四': 98, '王五': 45}
# 第一种方式,使用[]
print(scores['张三']) # 100
# print(scores['王二']) # KeyError: '王二'
# 第二种方式,使用get()
print(scores.get('张三')) # 100
print(scores.get('王二')) # None
print(scores.get('王二', 99)) # 99 设置默认值99,当key不存在时输出99
key的判断
使用 in、not in判断指定的key在字典中是否存在
in 指定的key在字典中存在返回True
not in 指定的key在字典中不存在返回True
# 获取字典的元素
scores = {'张三': 100, '李四': 98, '王五': 45}
print('张三' in scores)
print('张三' not in scores)
字典元素的删除与新增
# 删除字典的元素
scores = {'张三': 100, '李四': 98, '王五': 45}
print(scores)
del scores['张三'] # 删除指定的键值对
print(scores) # {'李四': 98, '王五': 45}
scores.clear() # 清空字典
print(scores) # {}
# 新增字典的元素,因为字典元素没有顺序这一说,所以没有列表元素那种与索引有关的删除与新增
scores = {'张三': 100, '李四': 98, '王五': 45} # 这里在pycharm中会有波浪线
scores['陈六'] = 90
print(scores) # {'张三': 100, '李四': 98, '王五': 45, '陈六': 90}
获取字典的视图
获取字典视图的三个方法:
keys():获取字典中所有的key
values():获取字典中所有的value
items():获取字典中所有的key,value对
scores = {'张三': 100, '李四': 98, '王五': 45}
print(scores)
# 获取所有的key
keys = scores.keys()
print(keys, type(keys)) # dict_keys(['张三', '李四', '王五']) <class 'dict_keys'>
# 将所有的key组成的视图转成列表
print(list(keys)) # ['张三', '李四', '王五']
# 获取所有的value
values = scores.values()
print(values, type(values)) # dict_values([100, 98, 45]) <class 'dict_values'>
# 将所有的value组成的视图转成列表
print(list(values)) # [100, 98, 45]
# 获取所有的键值对
items = scores.items()
print(items) # dict_items([('张三', 100), ('李四', 98), ('王五', 45)])
print(list(items)) # [('张三', 100), ('李四', 98), ('王五', 45)]
# 上一行输出的列表中的元素是由元组组成的
字典元素的遍历
scores = {'张三': 100, '李四': 98, '王五': 45}
# 字典元素的遍历
for item in scores:
print(item, scores[item], scores.get(item))
# 这里item遍历的是字典的键,要获得字典的值应使用[]、get()
字典的特点
①字典中的所有元素都是一个key-value对,key不允许重复,value可以重复
②字典中的元素是无序的
③字典中的key必须是不可变对象,字符串和整数是不可变对象,列表是可变对象
④字典可以根据需要动态的伸缩,列表也是动态的,不需要预先分配内存
⑤字典会浪费较大的内存,是一种使用空间换时间的数据结构
字典生成式
内置函数zip():用于将可迭代的对象作为参数,将对象中对应的元素打包成一个元组,然后返回由这些元组组成的列表
# zip的作用
items = ['fruits', 'books', 'others']
prices = [96, 78, 85]
lst = zip(items, prices)
print(list(lst)) # [('fruits', 96), ('books', 78), ('others', 85)]
# 注意这里还要用到list()函数
字典生成式
items = ['fruits', 'books', 'others']
prices = [96, 78, 85]
# 用字典生成式的形式生成一个字典
d = {item: price for item, price in zip(items, prices)}
print(d) # {'fruits': 96, 'books': 78, 'others': 85}
items = ['fruits', 'books', 'others', 'vegetables']
prices = [96, 78, 85]
d = {item: price for item, price in zip(items, prices)}
print(d) # {'fruits': 96, 'books': 78, 'others': 85}
items = ['fruits', 'books', 'others']
prices = [96, 78, 85, 70, 60]
d = {item: price for item, price in zip(items, prices)}
print(d) # {'fruits': 96, 'books': 78, 'others': 85}
# 会自动调整元素个数,以个数少的为准