1.通过关键字dict和关键字参数创建
diction = dict(dasan=1,xudachuan=2,bar=3)
print(diction)
print(type(diction))
{'dasan': 1, 'xudachuan': 2, 'bar': 3}
<class 'dict'>
2.直接创建
a = {"a":"b","c":"d"}
@还可以这样
d = {}
d["a"] ="b"
d["c"] = "d"
{'a': 'b', 'c': 'd'}
3.把两个列表合成字典(用zip)
list1 = ['你好', '他好', '大家好']; list2 = [1, 2, 3]; dict1 = dict(zip(list1, list2))
{'大家好': 3, '他好': 2, '你好': 1}
这个还没遇到:dict.fromkeys()
网友评论