Python案例100:列表转换为字典
作者:互联网
列表转换为字典
问题
题目:列表转换为字典。
https://www.runoob.com/python/python-exercise-example100.html
代码
- 代码
"""题目:列表转换为字典。"""
# 解题,dict()通过映射函数构造字典
l1=[1,2,3]
l2=["a","b","c"]
dic1=dict(zip(l1,l2))
print(dic1,type(dic1))
# dict()通过传入关键字构造字典
dict2=dict(a1='a', b1='b', t1='t')
print(dict2,type(dict2))
# dict()传入可迭代对象构造字典
dict3=dict([('one', 1), ('two', 2), ('three', 3)])
print(dict3,type(dict3))
- 运行结果
{1: ‘a’, 2: ‘b’, 3: ‘c’} <class ‘dict’>
{‘a1’: ‘a’, ‘b1’: ‘b’, ‘t1’: ‘t’} <class ‘dict’>
{‘one’: 1, ‘two’: 2, ‘three’: 3} <class ‘dict’>
dict()函数
- dict()通过传入关键字构造字典
dict2=dict(a1='a', b1='b', t1='t')
print(dict2,type(dict2))
- dict()通过映射函数构造字典
l1=[1,2,3]
l2=["a","b","c"]
dic1=dict(zip(l1,l2))
print(dic1,type(dic1))
- dict()传入可迭代对象构造字典
dict3=dict([('one', 1), ('two', 2), ('three', 3)])
print(dict3,type(dict3))
标签:dict3,dict2,Python,dict,print,100,type,字典 来源: https://blog.csdn.net/sinat_37960022/article/details/115112940