字典:
1 # 字典 2 # dict 3 # dict 4 # dic = { 5 # "k1": 'v1', 6 # "k2": 'v2' 7 # } 8 # 1 根据序列,创建字典,并指定统一的值 9 # v = dict.fromkeys(["k1",123,"999"],123) 10 # print(v) 11 12 # 2 根据Key获取值,key不存在时,可以指定默认值(None) 13 # v = dic['k2'] 14 # print(v) 15 # v = dic.get('k3',111111) 16 # print(v) 17 18 # 3 删除并获取值 19 # dic = { 20 # "k1": 'v1', 21 # "k2": 'v2' 22 # } 23 # v = dic.pop('k3',90) 24 # print(dic,v) 25 # k,v = dic.popitem() 26 # print(dic,k,v) 27 28 # 4 设置值, 29 # 已存在,不设置,获取当前key对应的值 30 # 不存在,设置,获取当前key对应的值 31 # dic = { 32 # "k1": 'v1', 33 # "k2": 'v2' 34 # } 35 # v = dic.setdefault('k1111','123') 36 # print(dic,v) 37 38 # 5 更新 39 # dic = { 40 # "k1": 'v1', 41 # "k2": 'v2' 42 # } 43 # dic.update({'k1': '111111','k3': 123}) 44 # print(dic) 45 # dic.update(k1=123,k3=345,k5="asdf") 46 # print(dic) 47 48 # 6 keys() 7 values() 8 items() get update 49 ########## 50 51 52 53 # 1、基本机构 54 # info = { 55 # "k1": "v1", # 键值对 56 # "k2": "v2" 57 # } 58 #### 2 字典的value可以是任何值 59 # info = { 60 # "k1": 18, 61 # "k2": True, 62 # "k3": [ 63 # 11, 64 # [], 65 # (), 66 # 22, 67 # 33, 68 # { 69 # 'kk1': 'vv1', 70 # 'kk2': 'vv2', 71 # 'kk3': (11,22), 72 # } 73 # ], 74 # "k4": (11,22,33,44) 75 # } 76 # print(info) 77 78 #### 3 布尔值(1,0)、列表、字典不能作为字典的key 79 # info ={ 80 # 1: 'asdf', 81 # "k1": 'asdf', 82 # True: "123", 83 # # [11,22]: 123 84 # (11,22): 123, 85 # # {'k1':'v1'}: 123 86 # 87 # } 88 # print(info) 89 90 # 4 字典无序 91 92 # info = { 93 # "k1": 18, 94 # "k2": True, 95 # "k3": [ 96 # 11, 97 # [], 98 # (), 99 # 22, 100 # 33, 101 # { 102 # 'kk1': 'vv1', 103 # 'kk2': 'vv2', 104 # 'kk3': (11,22), 105 # } 106 # ], 107 # "k4": (11,22,33,44) 108 # } 109 # print(info) 110 111 # 5、索引方式找到指定元素 112 # info = { 113 # "k1": 18, 114 # 2: True, 115 # "k3": [ 116 # 11, 117 # [], 118 # (), 119 # 22, 120 # 33, 121 # { 122 # 'kk1': 'vv1', 123 # 'kk2': 'vv2', 124 # 'kk3': (11,22), 125 # } 126 # ], 127 # "k4": (11,22,33,44) 128 # } 129 # v = info['k1'] 130 # print(v) 131 # v = info[2] 132 # print(v) 133 # v = info['k3'][5]['kk3'][0] 134 # print(v) 135 136 # 6 字典支持 del 删除 137 # info = { 138 # "k1": 18, 139 # 2: True, 140 # "k3": [ 141 # 11, 142 # [], 143 # (), 144 # 22, 145 # 33, 146 # { 147 # 'kk1': 'vv1', 148 # 'kk2': 'vv2', 149 # 'kk3': (11,22), 150 # } 151 # ], 152 # "k4": (11,22,33,44) 153 # } 154 # del info['k1'] 155 # 156 # del info['k3'][5]['kk1'] 157 # print(info) 158 159 # 7 for循环 160 # dict 161 # info = { 162 # "k1": 18, 163 # 2: True, 164 # "k3": [ 165 # 11, 166 # [], 167 # (), 168 # 22, 169 # 33, 170 # { 171 # 'kk1': 'vv1', 172 # 'kk2': 'vv2', 173 # 'kk3': (11,22), 174 # } 175 # ], 176 # "k4": (11,22,33,44) 177 # } 178 # for item in info: 179 # print(item) 180 # 181 # for item in info.keys(): 182 # print(item) 183 184 # for item in info.values(): 185 # print(item) 186 187 # for item in info.keys(): 188 # print(item,info[item]) 189 190 # for k,v in info.items(): 191 # print(k,v) 192 193 # True 1 False 0 194 # info ={ 195 # "k1": 'asdf', 196 # True: "123", 197 # # [11,22]: 123 198 # (11,22): 123, 199 # # {'k1':' v1'}: 123 200 # 201 # } 202 # print(info)