小白教程
所有教程
关于
Search
172.69.6.136
172.69.6.136
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Python func dict”的源代码
本页内容
上一节:
Python3_func_number_abs
下一节:
Python_func_help
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:dict()}}[[Category:python3 built in functions|2]] = Python dict() 函数 = [[python built in functions|Python 内置函数]] == 描述 == '''dict()''' 函数用于创建一个字典。 == 语法 == dict 语法: <sample title="" desc="" lang="python" hererun="1"> class dict(**kwarg) class dict(mapping, **kwarg) class dict(iterable, **kwarg) </sample> 参数说明: * **kwargs -- 关键字。 * mapping -- 元素的容器,映射类型(Mapping Types)是一种关联式的容器类型,它存储了对象与对象之间的映射关系。 * iterable -- 可迭代对象。 == 返回值 == 返回一个字典。 以下实例展示了 dict 的使用方法: <sample title="" desc="" lang="python" hererun="1"> >>>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} >>> </sample> === 只使用关键字参数创建字典 === <sample title="" desc="" lang="python" hererun="1"> numbers = dict(x=5, y=0) print('numbers =', numbers) print(type(numbers)) empty = dict() print('empty =', empty) print(type(empty)) </sample> 以上实例输出结果为: <sample title="" desc="" lang="python" hererun="1"> numbers = {'y': 0, 'x': 5} <class 'dict'> empty = {} <class 'dict'> </sample> === 使用可迭代对象创建字典 === <sample title="" desc="" lang="python" hererun="1"> # 没有设置关键字参数 numbers1 = dict([('x', 5), ('y', -5)]) print('numbers1 =',numbers1) # 设置关键字参数 numbers2 = dict([('x', 5), ('y', -5)], z=8) print('numbers2 =',numbers2) # zip() 创建可迭代对象 numbers3 = dict(dict(zip(['x', 'y', 'z'], [1, 2, 3]))) print('numbers3 =',numbers3) </sample> 以上实例输出结果为: <sample title="" desc="" lang="python" hererun="1"> numbers1 = {'y': -5, 'x': 5} numbers2 = {'z': 8, 'y': -5, 'x': 5} numbers3 = {'z': 3, 'y': 2, 'x': 1} </sample> === 使用映射来创建字典 === 映射类型(Mapping Types)是一种关联式的容器类型,它存储了对象与对象之间的映射关系。 <sample title="" desc="" lang="python" hererun="1"> numbers1 = dict({'x': 4, 'y': 5}) print('numbers1 =',numbers1) # 以下代码不需要使用 dict() numbers2 = {'x': 4, 'y': 5} print('numbers2 =',numbers2) # 关键字参数会被传递 numbers3 = dict({'x': 4, 'y': 5}, z=8) print('numbers3 =',numbers3) </sample> 以上实例输出结果为: <sample title="" desc="" lang="python" hererun="1"> numbers1 = {'x': 4, 'y': 5} numbers2 = {'x': 4, 'y': 5} numbers3 = {'x': 4, 'z': 8, 'y': 5} </sample> [[python built in functions|Python 内置函数]]
返回至“
Python func dict
”。
上一节:
Python3_func_number_abs
下一节:
Python_func_help