1 # 字典的创建 2 # 方式1.创建一个空字典 3 dic = {} 4 print(dic) 5 print(type(dic)) 6 7 # 方式2.赋值创建 8 dic = {'name': 'miaoge', 'age': 18} 9 print(dic) 10 print(type(dic)) 11 12 # 方式3.关键字dict和关键字参数创建 13 dic = dict(name='miaoge', age=18) 14 print(dic) 15 print(type(dic)) 16 17 # 方式4.把列表转成字典(列表内的元素要是元组) 18 l = [('name', 'miaoge'), ('age', 18)] 19 dic = dict(l) 20 print(dic) 21 print(type(dic)) 22 23 # 方式5.利用zip函数创建一个字典 24 re = zip('abc', [1, 2, 3]) 25 dic = dict(re) 26 print(dic) 27 print(type(dic)) 28 29 # 方式6.通过字典推导式创建 30 dic = {i: i*2 for i in range(10)} 31 print(dic) 32 print(type(dic)) 33 34 # 方式7.通过dict.fromkeys()创建 35 dic = dict.fromkeys('abcd', 'go for it') 36 print(dic) 37 print(type(dic)) 38 39 # 方式8.其他创建方式 40 l = ['x', 1, 'y', 2, 'z', 3] 41 dic = dict(zip(l[::2], l[1::2])) 42 print(dic) 43 print(type(dic))