解决Python 遍历字典时删除元素报异常的问题

原创 2017-01-16 13:45:22 338
摘要:错误的代码①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)其他文章!  


发布手记

热门词条

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号