字典
(待复习时自用)
昨天学的,没有用博客记录而已
dict1 = {'aa': 1, 'bb': 2}
print(dict1)
'''
字典键值的要求
1. 键的要求:唯一性,不可变性
2. 值: 可以是任意python支持的对象
'''
# 字典元素增加
dict1['cc'] = 3
dict1.setdefault('dd', 4)
print(dict1)
# 字典值查找
print(dict1['aa'])
print(dict1.get('bb'))
# 字典值修改
dict1['aa'] = 0
print(dict1)
dict1.update({'aa':1})
print(dict1)
# 字典元素的删除
del(dict1['aa'])
dict1.pop('bb')
print(dict1)
# 字典的遍历
# 遍历所有键值对
for i in dict1.items():
print(i)
# 遍历所有键
for j in dict1:
print(j)
for k in dict1.keys():
print(k)
# 遍历所有值
for get_v in dict1.values():
print(get_v)
# clean, copy, in与列表类似
# fromkeys给字典增加键,但是对应的值为空,None
dict2 = {}.fromkeys(['a', 'b', 'c'])
print(dict2)
最近真的好倒霉啊,小霉不断,也没有什么顺心事,啊啊啊,恶灵退散
难道有什么大好事要发生?