1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
| dict.get(key) 获取字典dict中键为key的值value
>>> dict1
{'name': 'Mei', 'lang': 'python'}
>>> dict1.get('name')
'Mei'
>>> dict1.get('lang')
'python'
dict.items() 返回字典dict的(key,value)元组对的列表的对象,可供用户去迭代访问所有的key和value
>>> dict1.items()
dict_items([('name', 'Mei'), ('lang', 'python')])
>>> for x,y in dict1.items():
... print('key is',x,',value is',y)
...
key is name ,value is Mei
key is lang ,value is python
dict.keys() 返回字典dict的key组成的列表的对象,可供用户去迭代访问所有的key
>>> dict1.keys()
dict_keys(['name', 'lang'])
>>> for x in dict1.keys():
... print('key is',x)
...
key is name
key is lang
dict.values() 返回字典dict的键值value组成的列表的对象,可供用户去迭代访问所有的value
>>> dict1={'name':'Mei','lang':'python'}
>>> dict1.values()
dict_values(['Mei', 'python'])
dict.pop(key[,returnValue]) 移除字典dict中键为key的键值对,并返回键key所对应的value的值
如果设置了returnValue的话,则当查找不到键key时,才返回returnValue值
>>> dict1={'a':3,'b':1,'c':2}
>>> dict1
{'a': 3, 'b': 1, 'c': 2}
>>> dict1.pop('a')
3
>>> dict1
{'b': 1, 'c': 2}
>>> dict1.pop('b')
1
>>> dict1
{'c': 2}
>>> dict1.pop('b','b is not the key')
'b is not the key'
>>> dict1
{'c': 2}
>>> dict1.pop('c','c is not the key')
2
>>> dict1
{}
dict.popitem() 移除字典dict中最后一个键值对,并返回被移除的键值对的值;
当dict字典为空时,使用popitem()方法会报错
>>> dict1={'a':3,'b':1,'c':2,'d':5,'e':4}
>>> dict1
{'a': 3, 'b': 1, 'c': 2, 'd': 5, 'e': 4}
>>> dict1.popitem()
('e', 4)
>>> dict1
{'a': 3, 'b': 1, 'c': 2, 'd': 5}
>>> dict1.popitem()
('d', 5)
>>> dict1
{'a': 3, 'b': 1, 'c': 2}
>>> dict1.popitem()
('c', 2)
>>> dict1
{'a': 3, 'b': 1}
>>> dict1.popitem()
('b', 1)
>>> dict1
{'a': 3}
>>> dict1.popitem()
('a', 3)
>>> dict1
{}
>>> dict1.popitem()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'popitem(): dictionary is empty'
dict.setdefault(key[,set_value]) 获取dict字典键key对应的value字
当key不存在时,若未指定set_value,则添加键值对key:None
当key不存在时,若指定set_value,则添加键值对key:set_value,并返回set_value
>>> dict1={'a':1,'b':2}
>>> dict1
{'a': 1, 'b': 2}
>>> dict1.setdefault('a')
1
>>> dict1.setdefault('a',3)
1
>>> dict1
{'a': 1, 'b': 2}
>>> dict1.setdefault('c')
>>> dict1
{'a': 1, 'b': 2, 'c': None}
>>> dict1.setdefault('c')
>>> dict1
{'a': 1, 'b': 2, 'c': None}
>>> dict1.setdefault('d','add_by_sedefault')
'add_by_sedefault'
>>> dict1
{'a': 1, 'b': 2, 'c': None, 'd': 'add_by_sedefault'}
>>> dict1.setdefault('b','b is not the key')
2
>>> dict1
{'a': 1, 'b': 2, 'c': None, 'd': 'add_by_sedefault'}
dict1.update(dict2) 按dict2更新dict1
如果dict2中的key值在dict1中存在,则将dict2中key对应的值赋值给dict1[key],即dict1[key]=dict2[key];
如果dict2中的key值在dict1中不存在,dict2[key]=value,则将dict2中key对应的键值对添加到字典dict1中,即dict1[key]=dict2[key];
>>> dict1={'a':1,'b':4,'c':2,'d':3,'f':5}
>>> dict1
{'a': 1, 'b': 4, 'c': 2, 'd': 3, 'f': 5}
>>> dict2={'a':5,'f':1}
>>> dict2
{'a': 5, 'f': 1}
>>> dict3={'d':2}
>>> dict3
{'d': 2}
>>> dict4={'b':'four','c':3}
>>> dict4
{'b': 'four', 'c': 3}
>>> dict1.update(dict2)
>>> dict1
{'a': 5, 'b': 4, 'c': 2, 'd': 3, 'f': 1}
>>> dict1.update(dict3)
>>> dict1
{'a': 5, 'b': 4, 'c': 2, 'd': 2, 'f': 1}
>>> dict1.update(dict3)
>>> dict1
{'a': 5, 'b': 4, 'c': 2, 'd': 2, 'f': 1}
>>> dict1.update(dict4)
>>> dict1
{'a': 5, 'b': 'four', 'c': 3, 'd': 2, 'f': 1}
>>> dict5={'g':6,'h':7}
>>> dict5
{'g': 6, 'h': 7}
>>> dict1.update(dict5)
>>> dict1
{'a': 5, 'b': 'four', 'c': 3, 'd': 2, 'f': 1, 'g': 6, 'h': 7}
>>> dict6={'b':4,'i':8}
>>> dict6
{'b': 4, 'i': 8}
>>> dict1
{'a': 5, 'b': 4, 'c': 3, 'd': 2, 'f': 1, 'g': 6, 'h': 7, 'i': 8}
>>> dict1.update({'j':9})
>>> dict1
{'a': 5, 'b': 4, 'c': 3, 'd': 2, 'f': 1, 'g': 6, 'h': 7, 'i': 8, 'j': 9}
dict.fromkeys(iterable, value=None) 生成一个新的字典
可迭代对象iterable可以是字符串、元组、列表或字典,用于创建字典的键key;
字典所有键key对应的同一值的初始值为value,用户不输入value值时,默认以None。
>>> dict1={'a':1,'b':2,'c':3}
>>> dict1
{'a': 1, 'b': 2, 'c': 3}
>>> dict1.fromkeys('123')
{'1': None, '2': None, '3': None}
>>> dict1.fromkeys('123','string字符串')
{'1': 'string字符串', '2': 'string字符串', '3': 'string字符串'}
>>> dict1.fromkeys((1,2,3),'string字符串')
{1: 'string字符串', 2: 'string字符串', 3: 'string字符串'}
>>> dict1.fromkeys((1,2,3),'tuple元组')
{1: 'tuple元组', 2: 'tuple元组', 3: 'tuple元组'}
>>> dict1.fromkeys(('1','2','3'),'tuple元组')
{'1': 'tuple元组', '2': 'tuple元组', '3': 'tuple元组'}
>>> dict1.fromkeys(['1','2','3'],'list列表')
{'1': 'list列表', '2': 'list列表', '3': 'list列表'}
>>> dict1.fromkeys([1,2,3],'list列表')
{1: 'list列表', 2: 'list列表', 3: 'list列表'}
>>> dict1.fromkeys({1:'a',2:'b',3:'c'},'dict字典')
{1: 'dict字典', 2: 'dict字典', 3: 'dict字典'}
>>> dict1.fromkeys({'1':'a','2':'b','3':'c'},'dict字典')
{'1': 'dict字典', '2': 'dict字典', '3': 'dict字典'}
dict.copy() 字典的浅拷贝,等同于copy模块中的copy()方法,进行浅拷贝
字典浅拷贝:深拷贝父对象(一级目录),子对象(二级目录)不拷贝,还是进行引用
copy模块中的deepcopy()方法为深拷贝,父对象和子对象同时会被拷贝。
>>> dict1={'a':1,'b':(1,2),'c':[3,['a','b'],5]}
>>> dict2=dict1 # 浅拷贝,仅引用对象
>>> dict3=dict1.copy() # 字典浅拷贝
>>> dict4=copy.copy(dict1) # copy模块浅拷贝
>>> dict5=copy.deepcopy(dict1) # copy模块深拷贝
>>> dict1
{'a': 1, 'b': (1, 2), 'c': [3, ['a', 'b'], 5]}
>>> dict2
{'a': 1, 'b': (1, 2), 'c': [3, ['a', 'b'], 5]}
>>> dict4
{'a': 1, 'b': (1, 2), 'c': [3, ['a', 'b'], 5]}
>>> dict3
{'a': 1, 'b': (1, 2), 'c': [3, ['a', 'b'], 5]}
>>> dict5
{'a': 1, 'b': (1, 2), 'c': [3, ['a', 'b'], 5]}
>>> dict1['c']
[3, ['a', 'b'], 5]
>>> dict1['c'][1]
['a', 'b']
>>> dict1['c'][1].remove('b')
>>> dict1
{'a': 1, 'b': (1, 2), 'c': [3, ['a'], 5]}
>>> dict2
{'a': 1, 'b': (1, 2), 'c': [3, ['a'], 5]}
>>> dict3
{'a': 1, 'b': (1, 2), 'c': [3, ['a'], 5]}
>>> dict4
{'a': 1, 'b': (1, 2), 'c': [3, ['a'], 5]}
>>> dict5
{'a': 1, 'b': (1, 2), 'c': [3, ['a', 'b'], 5]}
>>> dict1.pop('c')
[3, ['a'], 5]
>>> dict1
{'a': 1, 'b': (1, 2)}
>>> dict2
{'a': 1, 'b': (1, 2)}
>>> dict3
{'a': 1, 'b': (1, 2), 'c': [3, ['a'], 5]}
>>> dict4
{'a': 1, 'b': (1, 2), 'c': [3, ['a'], 5]}
>>> dict5
{'a': 1, 'b': (1, 2), 'c': [3, ['a', 'b'], 5]}
>>> adict={'姓名':'zhang','性别':['男','女']}
>>> adict
{'姓名': 'zhang', '性别': ['男', '女']}
>>> bdict=adict
>>> cdict=adict.copy()
>>> import copy
>>> ddict=copy.copy(adict)
>>> edict=copy.deepcopy(adict)
>>> adict
{'姓名': 'zhang', '性别': ['男', '女']}
>>> bdict
{'姓名': 'zhang', '性别': ['男', '女']}
>>> cdict
{'姓名': 'zhang', '性别': ['男', '女']}
>>> ddict
{'姓名': 'zhang', '性别': ['男', '女']}
>>> edict
{'姓名': 'zhang', '性别': ['男', '女']}
>>> adict['性别']
['男', '女']
>>> adict['性别'].remove('女')
>>> adict
{'姓名': 'zhang', '性别': ['男']}
>>> bdict
{'姓名': 'zhang', '性别': ['男']}
>>> cdict
{'姓名': 'zhang', '性别': ['男']}
>>> edict
{'姓名': 'zhang', '性别': ['男', '女']}
dict.__setitem__(key,value) 给dict字典的key赋值value,或添加新的key:value键值对
>>> dict1={'a':1,'b':(1,2)}
>>> dict1
{'a': 1, 'b': (1, 2)}
>>> dict1.__setitem__('c',3)
>>> dict1
{'a': 1, 'b': (1, 2), 'c': 3}
>>> dict1.__setitem__('b',None)
>>> dict1
{'a': 1, 'b': None, 'c': 3}
dict.clear() 清空字典
>>> dict1={'a':1,'b':(1,2)}
>>> dict1
{'a': 1, 'b': (1, 2)}
>>> dict1.clear()
>>> dict1
{}
dict(other) 使用dict()将其他类型的双值子系列转换成字典
# 包含双值列表的列表
>>> list1 = [['a',1],['b',2],['c',3]]
>>> list1
[['a', 1], ['b', 2], ['c', 3]]
>>> dict(list1)
{'a': 1, 'b': 2, 'c': 3}
>>>
# 包含双值元组的列表
>>> list2 = [('a','b'),('c','d'),('e','f')]
>>> dict(list2)
{'a': 'b', 'c': 'd', 'e': 'f'}
>>>
# 包含双值列表的元组
>>> tuple1 = (['a','b'],['c','d'],['e','f'])
>>> dict(tuple1)
{'a': 'b', 'c': 'd', 'e': 'f'}
>>>
# 双字符的字符串组成的列表
>>> list1 = ['ab','cd','ef']
>>> dict(list1)
{'a': 'b', 'c': 'd', 'e': 'f'}
>>>
# 双字符的字符串组成的元组
>>> tuple1 = ('ab','cd','ef')
>>> dict(tuple1)
{'a': 'b', 'c': 'd', 'e': 'f'}
>>>
|