#字典学习 可变容器模型
# 创建字典的字面量语法
scores = {'刘德华': 95, '白元芳': 78, '狄仁杰': 82}
print(scores) # {'刘德华': 95, '白元芳': 78, '狄仁杰': 82}
# 创建字典的构造器语法
items1 = dict(one=1, two=2, three=3, four=4)
# 通过zip函数将两个序列压成字典
items2 = dict(zip(['a', 'b', 'c'], '123'))
# 创建字典的推导式语法
items3 = {num: num ** 2 for num in range(1, 10)}
print(items1, items2, items3) #{'one': 1, 'two': 2, 'three': 3, 'four': 4} {'a': '1', 'b': '2', 'c': '3'} {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
# 通过键可以获取字典中对应的值
print(scores['刘德华']) #95
print(scores['狄仁杰']) #82
# 对字典中所有键值对进行遍历
for key in scores:
print(f'{key}: {scores[key]}')
#刘德华: 95
#白元芳: 78
#狄仁杰: 82
# 更新字典中的元素
scores['白元芳'] = 65
scores['诸葛王朗'] = 71
scores.update(冷面=67, 方启鹤=85)
print(scores) #{'刘德华': 95, '白元芳': 65, '狄仁杰': 82, '诸葛王朗': 71, '冷面': 67, '方启鹤': 85}
if '武则天' in scores:
print(scores['武则天']) #
print(scores.get('武则天')) #None
# get方法也是通过键获取对应的值但是可以设置默认值
print(scores.get('武则天', 60)) #60
# 删除字典中的元素
print(scores.popitem()) #('方启鹤', 85)
print(scores.popitem()) #('冷面', 67)
print(scores.pop('33', 100)) #100
# 清空字典
scores.clear()
print(scores) #{}