python dict函数

dict()函数用于创建一个字典,返回值为一个字典

dict()语法

class dict(**kwarg)  //**kwarg  关键字
class dict(mapping, **kwarg)   //mapping 元素的容器
class dict(iterable, **kwarg)  //iterable 可迭代对象


实例用法
dict() //创建空字典
dict(a='a',b='b',c='c') //传入关键字
dict(zip(['one','two','three'],[1,2,3])) //映射函数方式构造字典
dict([('one',1),('two',2),('three',3)]) //可迭代对象方式构造字典

>>>dict()# 创建空字典

{}

>>> dict(a='a', b='b', t='t')# 传入关键字

{'a': 'a', 'b': 'b', 't': 't'}

>>> dict(zip(['one', 'two', 'three'], [1, 2, 3]))# 映射函数方式来构造字典

{'three': 3, 'two': 2, 'one': 1}

>>> dict([('one', 1), ('two', 2), ('three', 3)])# 可迭代对象方式来构造字典

{'three': 3, 'two': 2, 'one': 1} >>>

原文地址:https://www.cnblogs.com/LM791605490/p/15071738.html