码迷,mamicode.com
首页 > 编程语言 > 详细

Python标准库:内置函数dict(iterable, **kwarg)

时间:2014-11-20 22:01:50      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:python

本函数是从可迭代对象来创建新字典。比如一个元组组成的列表,或者一个字典对象。

例子:

#dict()
#以键对方式构造字典
d1 = dict(one = 1, two = 2, a = 3)
print(d1)

#以映射函数方式来构造字典
d2 = dict(zip([‘one‘, ‘two‘, ‘three‘], [1, 2, 3]))
print(d2)

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

d4 = dict(d3)
print(d4)

输出结果如下:

{‘a‘: 3, ‘two‘: 2, ‘one‘: 1}

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

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

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



蔡军生 QQ:9073204 深圳

Python标准库:内置函数dict(iterable, **kwarg)

标签:python

原文地址:http://blog.csdn.net/caimouse/article/details/41320951

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!