1.描述
dict()函数用于创造一个字典。
2.语法
class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)
**kwargs – 关键字
mapping – 元素的容器。
iterable – 可迭代对象。
3.返回值
返回一个字典。
4.实例
(1)直接创造字典,有两种办法:
dict01 = dict() 或者是 dict01 = {} python中支持的两种方式。
(2)赋值方式1:
dict01 = {}
dict01[firstname] = ‘yun’
dict01[lastname] = ‘nan’
(3)赋值方式2:
dict01 = {‘lastname’:‘da’,‘firstname’:‘li’ }
(4)赋值方式3:
dict01 =dict(‘lastname’=‘da’,‘firstname’=‘li’)
(5)赋值方式4:
可迭代对象方式来构造字典
dict01 = dict([(‘one’, 1), (‘two’, 2), (‘three’, 3)])
{‘three’: 3, ‘two’: 2, ‘one’: 1}
(6)赋值方式5:
映射函数方式来构造字典
dict01 = dict(zip([‘one’, ‘two’, ‘three’], [1, 2, 3]))
{‘three’: 3, ‘two’: 2, ‘one’: 1}