摘要:错误的代码①d = {'a':1, 'b':0, 'c':1, 'd':0} for key, val in d.items(): del(d[k])错误的代码② -- 对于Python3d = {
错误的代码①
d = {'a':1, 'b':0, 'c':1, 'd':0}
for key, val in d.items():
del(d[k])错误的代码② -- 对于Python3
d = {'a':1, 'b':0, 'c':1, 'd':0}
for key, val in d.keys():
del(d[k])正确的代码
d = {'a':1, 'b':0, 'c':1, 'd':0}
keys = list(d.keys())
for key, val in keys:
del(d[k])更多关于解决Python 遍历字典时删除元素报异常的问题请关注PHP中文网(www.php.cn)其他文章!