创建字典的方法
1 ### 1 直接创建 2 ```python 3 dict = {'name':'earth', 'port':'80'} 4 ``` 5 ### 2 工厂方法 6 ```python 7 items=[('name','earth'),('port','80')] 8 dict2=dict(items) 9 dict1=dict((['name','earth'],['port','80'])) 10 ``` 11 ### 3 fromkeys()方法 12 ```python 13 dict1={}.fromkeys(('x','y'),-1) 14 dict={'x':-1,'y':-1} 15 dict2={}.fromkeys(('x','y')) 16 dict2={'x':None, 'y':None} 17 ```
下面用途map来实现:
1 # a,b=(("a"),("b")),(("c"),("d")) 2 # c=map(lambda x,y:{x:y}, a,b) 3 # print(list(c)) #[{'a': 'c'}, {'b': 'd'}]