一、字典相关运算方法
1.clear
字典.clear() - 清空字典(删除字典中所有的键值对)
dict1 = {'a':1,'b':2}
dict1.clear()
2.copy
字典.copy()- 复制字典中的所有的键值对 产生一个新的字典
dict1 = {'a':1,'b':2}
dict2 = dict1.copy()
3.fromkeys
dict.fromkeys(序列,值)- 把序列中的元素作为key,值作为所有key对应的默认值,创建一个新的字典
dict3= dict.fromkeys('abc',100)
print(dict3) # {'a': 100, 'b': 100, 'c': 100}
dict3= dict.fromkeys(['name','age','hah'],100)
print(dict3) # {'name': 100, 'age': 100, 'hah': 100}
4.get
字典.get(key)- 获取key对应的值,不存在返回None
字典.get(key,值)- 获取key对应的值,如果key不存在 返回指定的值
dict3 = {'name': 100, 'age': 100, 'hah': 100}
print(dict3.get('name1')) #None
p