zoukankan      html  css  js  c++  java
  • python错误之RuntimeError: dictionary changed size during iteration

    pythonn报错信息:
    C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/Administrator/PycharmProjects/pythondemo/maptest.py
    Traceback (most recent call last):
    File "C:/Users/Administrator/PycharmProjects/pythondemo/maptest.py", line 5, in <module>
    for line in maps.keys():
    RuntimeError: dictionary changed size during iteration

    python2中实现遍历的同时删除字典中的元素;python3中运行报错信息:
    C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/Administrator/PycharmProjects/pythondemo/maptest.py
    Traceback (most recent call last):
      File "C:/Users/Administrator/PycharmProjects/pythondemo/maptest.py", line 5, in <module>
        for line in maps.keys():
    RuntimeError: dictionary changed size during iteration
    
    # maps = {1:"李明",2:"丹尼"}
    # for line in maps.keys():
    #     if(line == 2):
    #         maps.pop(line)
    # print(maps)
    

      

    python3中实现遍历的同时删除字典中的元素
    maps = {1:"李明",2:"丹尼"}
    for line in list(maps.keys()):
        if(line == 2):
            maps.pop(line)
    print(maps)
    

      

  • 相关阅读:
    Java线程面试题 Top 50
    深入理解java内存模型
    线程池的理解
    Thread的理解
    ThreadLocal的理解
    排序算法(简)
    排序算法
    【Java集合源码剖析】LinkedHashmap源码剖析
    vsftp上传文件权限问题
    xp 通过注册表修改环境变量
  • 原文地址:https://www.cnblogs.com/g177w/p/err1.html
Copyright © 2011-2022 走看看