Python快速创建词典

2019-09-20
#Python

一、使用大括号来创建字典

person  = {"name":"张三","age":18,"pay":40000,"job":"Python工程师"}

二、使用关键字参数和类型构造函数来创建字典

person = dict(name="张三",age=18,pay=40000,job="Python工程师")

三、使用zip函数将名/值列表链接在一起,来创建字典

keys = ["name","age","pay","job"]
values = ["张三",18,40000,"Python工程师"]
person = dict(zip(keys,values))

四、使用fromkeys通过一个键序列和所有键的可选初始值来创建字典

keys = ["name","age","pay","job"]
person = dict.fromkeys(keys,"?")

来源