# Python学习
# 学习时间: 2022/7/3 13:56
scores={'张三':100,'李四':98,'王五':45}
for item in scores:
print(item,scores[item],scores.get(item)) # 获取到键
# Python学习
# 学习时间: 2022/6/30 17:56
'''通过[]或者通过get()'''
'''[]如果字典中不存在指定的key,抛出keyError异常'''
'''get()取值,如果字典中不存在指定的key,并不会抛出keyError而是返回None,可以通过参数设置默认的value,以便指定的key不存在时返回'''
scores={'张三':100,'李四':98,'王五':45}
print(scores['张三'])
# print(scores['陈六'])
''''第二种方式: 使用get()'''
print(scores.get('张三'))
print(scores.get('陈六')) # 差别在于 get 输出None而不报错
print(scores.get('天天',99))
# Python学习
# 学习时间: 2022/6/30 18:07
scores={'张三':100,'李四':98,'王五':45}
keys=scores.keys()
values=scores.values()
items=scores.items()
print('——————— —keys————————')
print(keys)
print(type(keys))
print(list(keys))
print('————————value————————')
print(values)
print(type(values))
print(list(values))
print('————————items————————')
print(items)
print(type(items))
print(list(items)) # 转换后的列表元素由元组组成
# Python学习
# 学习时间: 2022/6/30 14:45
# 字典:以键对值的方式存储数据,字典是一个无序的序列
# 字典与hash表类似
# 字典的实现原理:根据key查找所在的value
'''第一种创建: 使用{}创建字典'''
scores={'张三':100,'李四':98,'王五':45}
print(scores)
print(type(scores))
'''第二种创建dict()'''
student=dict(name='jack',age=20)
print(student)
'''空字典'''
d={}
print(d)
# Python学习
# 学习时间: 2022/7/3 15:17
# 内置函数zip():将可迭代的对象作为参数,将对象中对应的元素打包成一个元祖,然后由这些元组组成的列表
items={'Fruits','Books','Others'}
prices=[96,85,78]
list2={item.upper():price for item ,price in zip(items,prices)}
print(list2)
# Python学习
# 学习时间: 2022/7/3 15:11
# 字典的特点
# 字典中的所有元素都是一个key-value对,key不允许重复,value可以重复
# 错误示范: 后面的键值对将前面的键值对取代
d={'name':'张三','name':'李四'}
print(d)
# 字典中的元素是无序的
# 字典中的key必须是不可变对象
# 字典可以根据需要动态地伸缩
# 字典会浪费较大的内存,是一种使用空间换时间的数据结构
# Python学习
# 学习时间: 2022/6/30 18:04
scores={'张三':100,'李四':98,'王五':45}
print('张三' in scores)
print('陈六' not in scores)
del scores['张三'] # 只清除张三
# scores.clear() 字典元素的清空
scores['陈六']=100
scores['陈六']=99
print(scores)