One - One Code All

Blog Content

python遍历字典并修改,错误:dictionary changed size during iteration

Python   2010-06-16 00:04:32

ython遍历字典并修改,错误:dictionary changed size during iteration

参考:https://www.python.org/dev/peps/pep-0234/
大概是说字典在遍历时不能进行修改,建议转成列表或集合处理。

解决:把字典的keys变成list。

a={'a':1, 'b':0, 'c':1, 'd':0}
for key in list(a.keys()):
    del a[key]



上一篇:R常用函数-rnorm正态分布
下一篇:Python之math向上取整、向下取整以及四舍五入函数

The minute you think of giving up, think of the reason why you held on so long.