原始代码:
需求:遍历data字典,将值为‘无’的键值对进行删除
for key, value in data.items():
if value == '无':
data.pop(key)
else:
pass
报错:RuntimeError: dictionary changed size during iteration
原因:字典不支持在遍历时进行修改值,将其改为list格式
如下:
for key, value in list(data.items()):
if value == '无':
data.pop(key)
else:
pass