首页 > 编程语言 > 详细

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

时间:2014-11-20 22:00:35      阅读:206      评论:0      收藏:0      [点我收藏+]

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

例子:

#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)

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!