菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 基础教程
(追記) (追記ここまで)

Python dict() 函数

Python 内置函数 Python 内置函数


描述

dict() 函数用于创建一个字典。

语法

dict 语法:

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

参数说明:

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

返回值

返回一个字典。

实例

以下实例展示了 dict 的使用方法:

>>>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} >>>

只使用关键字参数创建字典

实例

numbers = dict(x=5, y=0)
print('numbers =', numbers)
print(type(numbers))

empty = dict()
print('empty =', empty)
print(type(empty))

以上实例输出结果为:

numbers = {'y': 0, 'x': 5}
<class 'dict'>
empty = {}
<class 'dict'>

使用可迭代对象创建字典

实例

# 没有设置关键字参数
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)

以上实例输出结果为:

numbers1 = {'y': -5, 'x': 5}
numbers2 = {'z': 8, 'y': -5, 'x': 5}
numbers3 = {'z': 3, 'y': 2, 'x': 1}

使用映射来创建字典

映射类型(Mapping Types)是一种关联式的容器类型,它存储了对象与对象之间的映射关系。

实例

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)

以上实例输出结果为:

numbers1 = {'x': 4, 'y': 5}
numbers2 = {'x': 4, 'y': 5}
numbers3 = {'x': 4, 'z': 8, 'y': 5}

Python 内置函数 Python 内置函数

AI 思考中...

3 篇笔记 写笔记

  1. #0

    myelsa

    945***[email protected]

    20

    利用 dict(([key,value],[key,value])) 的方式创建字典:

    >>> dict((['a',2],['b',3]))
    {'a': 2, 'b': 3}

    myelsa

    945***[email protected]

    8年前 (2018年11月29日)
  2. #0

    wujianai

    wuj***[email protected]

    86

    创建字典的 5 种方式。

    方式 1:

    >>> dict1 = {}
    >>> dict1['firstname'] = 'ma'
    >>> dict1['lastname'] = 'yun'

    方式 2:

    >>> dict1 = {'firstname':'ma', 'lastname':'yun'}

    方式 3:

    >>> dict1 = dict([('firstname','ma'), ('lastname', 'yun')])

    方式 4:

    >>> dict1 = dict(firstname = 'ma', lastname = 'yun')

    方式 5:

    >>> dict1 = dict(zip(['firstname', 'lastname'], ['ma', 'yun']))

    wujianai

    wuj***[email protected]

    7年前 (2019年07月22日)
  3. #0

    Max

    105***[email protected]

    58

    dict()创建字典应该是6种方式。

    dict()创建字典应该是6种方式。

    方式 1:

    >>> dict1 = {}
    >>> dict1['firstname'] = 'ma'
    >>> dict1['lastname'] = 'yun'

    方式 2:

    >>> dict1 = {'firstname':'ma', 'lastname':'yun'}

    方式 3:

    >>> dict1 = dict(firstname = 'ma', lastname = 'yun')

    方式 4:

    >>> dict1 = dict([('firstname','ma'), ('lastname', 'yun')])

    方式 5:

    >>> dict1 = dict((['firstname','ma'], ['lastname', 'yun']))

    方式6:

    >>> dict1 = dict(zip(['firstname', 'lastname'], ['ma', 'yun']))

    Max

    105***[email protected]

    7年前 (2020年01月09日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /