1、字典的定义及原理
# Name: Python study
# Designer: MilesHugh
# Time: 2023/4/7 15:17
# 字典{}
# -Python内置的数据结构之一,与列表[]一样是一个可变序列
# -以键值对的方式存储数据,字典是一个无序的序列
# 字典形式:
# scores = {'XXX': 1, 'YYY': 2, 'ZZZ': 3}
# 字典名 = {键:值,键:值}
# 存入字典中,需要经过hash()函数工序去计算得到value对应的位置信息,需要不可变序列(不能进行增删改操作)
# -Python中的字典是根据key查找value所在的位置
2、字典的创建
# Name: Python study
# Designer: MilesHugh
# Time: 2023/4/7 16:05
# 字典dictionary的创建
# -使用花括号
# -使用内置函数dict()
print('------使用{}创建字典------')
scores = {'张三': 100, '李四': 98, '王五': 45}
print(scores, type(scores))
# 数据类型为:字典dict
print('------使用内置函数dict()创建字典------')
student = dict(name='jack', age=20)
print(student, type(student))
print('------创建空字典------')
d = {}
print(d, type(d))
3、字典的常用操作
# Name: Python study
# Designer: MilesHugh
# Time: 2023/4/7 16:07
# 字典的常用操作
# -字典中元素的获取
# -[]取值与使用get()取值的区别
# -key的判断
# -字典元素的删除
# -字典元素的新增
# -字典元素的修改
# -获取字典视图
# -字典元素的遍历
print('------字典中元素的获取------')
scores = {'张三': 100, '李四': 98, '王五': 45}
print('---第一种方式,使用[]---')
print(scores['张三'])
# print(scores['陈六']) 不存在,会报错 #KeyError: '陈六'
print('---第二种方式,使用get()方法---')
print(scores.get('李四'))
print(scores.get('陈六'))
# None,不报错
print(scores.get('麻七', 99))
# 99是在查找'麻七'所对的value不存在时,提供的一个默认值
print('------key的判断------')
scores = {'张三': 100, '李四': 98, '王五': 45}
print('张三' in scores)
print('张三' not in scores)
print('------字典元素的删除------')
del scores['张三'] # 删除指定的key-value对
print(scores)
print('------清空字典的元素------')
scores.clear()
print(scores)
print('------字典元素的新增------')
scores['陈六'] = 98
print(scores)
print('------字典元素的修改------')
scores['陈六'] = 100
print(scores)
print('------获取字典视图------')
scores = {'张三': 100, '李四': 98, '王五': 45}
print('---获取所有的key---')
keys = scores.keys()
print(keys)
print(type(keys))
print(list(keys))
# 将所有的key组成的视图转成列表
print('---获取所有的value---')
values = scores.values()
print(values)
print(type(values))
print(list(values))
# 将所有的value组成的视图转成列表
print('---获取所有的key-value对---')
items = scores.items()
print(items)
print(list(items))
# 元组(,)
# 转换之后的列表元素是由元组组成
print('------字典元素的遍历------')
scores = {'张三': 100, '李四': 98, '王五': 45}
for item in scores:
print(item, scores[item], scores.get(item))
# scores[item]和scores.get(item)都可以根据key找到对应的value
# 区别在于scores[item]会出现报错异常,而scores.get(item)不会
4、字典的特点
# Name: Python study
# Designer: MilesHugh
# Time: 2023/4/7 16:23
# 字典的特点
# -字典中的所有元素都是一个key-value对,key不允许重复,value可以重复
# -字典中的元素是无序的
# -字典中的key必须是不可变对象
# -字典也可以根据需要动态地伸缩
# -字典会浪费较大的内存,是一种使用“空间换时间”的数据结构
print('------字典中key和value的重复问题------')
# d = {'name': '张三', 'name': '李四'}
# print(d)
# key不允许重复,后面的value会将前面的value覆盖
d = {'name': '张三', 'nikename': '张三'}
# value可以重复的
print(d)
print('------字典中元素是无序的------')
lst = [10, 20, 30]
lst.insert(1, 100)
print(lst)
print('------字典中的key必须是不可变对象------')
# d = {lst: 100}
# print(d)
# 会报错,因为列表lst是可变对象
5、字典生成式
# Name: Python study
# Designer: MilesHugh
# Time: 2023/4/7 16:31
# 字典生成式
# -内置函数zip():
# 用于将可迭代的对象作为参数,将对象中对应的元素打包成一个元组,然后返回由这些元组组成的列表
# -表达式:{item.upper(): price for item, price in zip(items, prices)}
# -item.upper()表示字典key的表达式
# -price表示字典value的表达式
# -item自定义表示key的变量
# -price自定义表示value的变量
# -zip(items, prices)可迭代对象
items = ['Fruits', 'Books', 'Trees',]
prices = [96, 78, 85, 100, 120]
d = {item: price for item, price in zip(items, prices)}
print(d)
items = ['Fruits', 'Books', 'Trees']
prices = [96, 78, 85, 100, 120]
d = {item.upper(): price for item, price in zip(items, prices)}
# 使得所有的key都大写
print(d)
items = ['Fruits', 'Books', 'Trees', 'Vegetables']
prices = [96, 78, 85, 100, 120]
d = {item.upper(): price for item, price in zip(items, prices)}
print(d)