Python 字典 dict() 函数

描述

Python 字典 dict() 函数用于创建一个新的字典,用法与 Pyhon 字典 update() 方法相似。

语法

dict() 函数函数语法:

dict(key/value)

参数说明:

  • key/value -- 用于创建字典的键/值对,此处可以表示键/值对的方法有很多,请看实例。

返回值

返回一个新的字典。

实例

以下实例展示了 dict() 函数的使用方法:

# !/usr/bin/python3

dict0 = dict()  # 传一个空字典
print('dict0:', dict0)

dict1 = dict({'three': 3, 'four': 4})  # 传一个字典
print('dict1:', dict1)

dict2 = dict(five=5, six=6)  # 传关键字
print('dict2:', dict2)

dict3 = dict([('seven', 7), ('eight', 8)])  # 传一个包含一个或多个元祖的列表
print('dict3:', dict3)

dict5 = dict(zip(['eleven', 'twelve'], [11, 12]))  # 传一个zip()函数
print('dict5:', dict5)

以上实例输出结果为:

dict0: {}
dict1: {'four': 4, 'three': 3}
dict2: {'five': 5, 'six': 6}
dict3: {'seven': 7, 'eight': 8}
dict5: {'twelve': 12, 'eleven': 11}

zip() 函数:http://www.cnblogs.com/wushuaishuai/p/7766470.html

原文地址:https://www.cnblogs.com/wushuaishuai/p/7678210.html