python内置函数dict()

一、简介

简而言之,dict()内置函数就是用来创建一个字典

二、详解

语法:

class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)

有三种形式,其中参数的意义是

  • **kwargs – 关键字。
  • mapping – 元素的容器,映射类型(Mapping Types)是一种关联式的容器类型,它存储了对象与对象之间的映射关系。
  • iterable – 可迭代对象。

三、代码

举例:

dict1 = dict()  # 创建空字典
print(dict1)

dict2 = dict(a=1, b=1, c=1)  # 用关键字参数
print(dict2)

dict3 = dict(zip(['one', 'two', 'three'], [1,2,3]))  # 用mapping映射函数的方式
print(dict3)

dict4 = dict([('one', 1), ('two', 2), ('three', 3)])  # 用iterable可迭代对象的方式
print(dict4)
{}
{'a': 1, 'b': 1, 'c': 1}
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': 3}

四、Reference

https://www.runoob.com/python/python-func-dict.html
https://docs.python.org/zh-cn/3.9/library/functions.html#func-dict

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐