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

it2025-09-10  51

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

样例:

#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 深圳

转载于:https://www.cnblogs.com/bhlsheji/p/4287489.html

相关资源:数据结构—成绩单生成器
最新回复(0)