Python基础数据类型dict

2018-11-19  本文已影响0人  三人行大道

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

上一篇下一篇

猜你喜欢

热点阅读