字典fromkeys方法和update方法

时间:2023-03-09 22:56:50
字典fromkeys方法和update方法
 #Author : Kelvin
#Date : 2019/1/17 15:27 #字典的update方法,是向调用者字典中添加另外一个字典
dict1 = {"name":"kelvin", "age": 22}
dict2 = {"sex":"male"}
dict1.update(dict2)
print(dict1)
#>>>: {'name': 'kelvin', 'age': 22, 'sex': 'male'} #字典的fromkeys方法
list1=["kelvin","bob","jam"]
new_dict1=dict.fromkeys(list1,"good")
print(new_dict1)
#>>>: {'kelvin': 'good', 'bob': 'good', 'jam': 'good'}
new_dict2=dict.fromkeys(list1)
print(new_dict2)
#>>>: {{'kelvin': None, 'bob': None, 'jam': None}