字典fromkeys方法和update方法

 1 #Author : Kelvin
 2 #Date : 2019/1/17 15:27
 3 
 4 #字典的update方法,是向调用者字典中添加另外一个字典
 5 dict1 = {"name":"kelvin", "age": 22}
 6 dict2 = {"sex":"male"}
 7 dict1.update(dict2)
 8 print(dict1)
 9 #>>>: {'name': 'kelvin', 'age': 22, 'sex': 'male'}
10 
11 
12 #字典的fromkeys方法
13 list1=["kelvin","bob","jam"]
14 new_dict1=dict.fromkeys(list1,"good")
15 print(new_dict1)
16 #>>>: {'kelvin': 'good', 'bob': 'good', 'jam': 'good'}
17 new_dict2=dict.fromkeys(list1)
18 print(new_dict2)
19 #>>>: {{'kelvin': None, 'bob': None, 'jam': None}
原文地址:https://www.cnblogs.com/sun-10387834/p/10282561.html