在做对员工信息增删改查这个作业时,有一个需求是通过用户输入的id删除用户信息。我把用户信息从文件提取出来储存在了字典里,其中key是用户id,value是用户的其他信息。在循环字典的时候,当用户id和字典里的key相等时,会删除这条信息,当时删除时报错RuntimeError: dictionary changed size during iteration。

for key in staff_info:
if user_id == key:
print(key)
staff_info.pop(key)
id_exist = True

参考:https://www.python.org/dev/peps/pep-0234/#dictionary-iterators

在官网看到解释:

Dictionaries implement a tp_iter slot that returns an efficient iterator that iterates over the keys of the dictionary. During such an iteration, the dictionary should not be modified, except that setting the value for an existing key is allowed (deletions or additions are not, nor is the update() method). This means that we can write

for k in dict: ...

which is equivalent to, but much faster than

for k in dict.keys(): ...

as long as the restriction on modifications to the dictionary (either by the loop or by another thread) are not violated.

意思是多字典在被循环的时候不能被循环删除和更新,除了给一个已经存在的key设置value。还说道 for kin dict: ...和for k in dict.keys(): ...效果是一样的,但是前者速度更快。

那就来试验一下:

a = {'a': 1, 'b': 0, 'c': 1, 'd': 0}

for key in a:
print(key, a[key]) for key in a.keys():
print(key, a[key]) # 两个循环效果一样 print(a.keys()) # dict_keys(['a', 'b', 'c', 'd'])
print(a) # {'a': 1, 'b': 0, 'c': 1, 'd': 0}

除了a.keys是dict_keys类型,它们的效果是一样的。

那么解决方案就出来了,转换成列表形式就OK了。

for key in list(staff_info.keys()):
if user_id == key:
print(key)
staff_info.pop(key)
id_exist = True
有道词典

for k in dict: ...

详细X

k的dict类型:…

循环字典进行操作时出现:RuntimeError: dictionary changed size during iteration的解决方案的更多相关文章

  1. 迭代var()内置函数的时候出现RuntimeError: dictionary changed size during iteration的解决办法

    下午看了Mr Seven的教学视频,其中有一段讲全局变量的视频,迭代输出全局变量的时候报错了. 视频中的做法: for k,v in vars().items(): print(k) 打印结果 for ...

  2. python错误之RuntimeError: dictionary changed size during iteration

    pythonn报错信息: C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/Ad ...

  3. python 报错RuntimeError: dictionary changed size during iteration

    a = {':0} for b in list(a.keys()): if a[b] == 0: del a[b] print(a) 报错是因为在字典迭代期间改变字典大小 我们可以通过取出字典的键值, ...

  4. Python面试题目之(针对dict或者set数据类型)边遍历 边修改 报错dictionary changed size during iteration

    # result 是一个字典, 把里面属性值是None的属性删除 for key in result: if not result[key]: del result[key] continue 但是报 ...

  5. python : dictionary changed size during iteration

    1. 错误方式 #这里初始化一个dict >>> d = {'a':1, 'b':0, 'c':1, 'd':0} #本意是遍历dict,发现元素的值是0的话,就删掉 >> ...

  6. Python字典的操作与使用

    字典的描述 字典是一种key-value的数据类型,使用就像我们上学用的字典,通过拼音(key)来查对应字的详细内容(value). 字典的特性 1.字典是无序的(不像列表一样有下标,它通过key来获 ...

  7. Python—字典的操作

    字典的操作: #字典的本质其实是dict类的对象 >>> a = dict([(")]) >>> a {'} 一.增加 >>> stud ...

  8. python中字典的操作

    ----------字典操作------------ --查字典1. 字典名["元素名称"]2. 字典名.get("元素名称")-获取不存在得元素名称,.get ...

  9. Python学习杂记_6_字典常用操作

    字典操作 字典是由一对花括号括起来的一组“键值对”,每个键值对就是字典的一个元素,元素在字典中是无序的,常见操作如下: info = { 'name':'xiaoming', 'sex':'nan', ...

随机推荐

  1. byte取高4位,低4位,byte转int

    byte abyte =-1; System.out.println(abyte); System.out.println(Integer.toBinaryString(abyte)); //取高四位 ...

  2. Hive_Hive的数据模型_外部表

    Hive的数据模型之外部表 外部表(External Table)- 指向已经在HDFS中存在的数据,可以创建Partition- 它和内部表在元数据的组织上是相同的,而实际数据的存储则有较大的差异. ...

  3. HTML——表单

    总结: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  4. openstack安装newton版本Glance部署(二)

    一.部署Glance 1.Glance 安装 [root@linux-node1 ~]#yum install openstack-glance python-glance python-glance ...

  5. 在MasterPage中检验session是否存在~

    在母板頁中檢查user是否登入過,這樣就不用在每個頁中去作檢驗.在其Init事件中寫入如下代碼:     protected void ContentPlaceHolder1_Init(object  ...

  6. Mybatis 使用注解和Provider类实现动态条件查询

    1.注解内拼写 Mybatis SQL 脚本 @Repository public interface CustomerFeedMapper extends BaseCrudMapper<Cus ...

  7. 洛谷 CF1148A Another One Bites The Dust

    Another One Bites The Dust CF的题目在你谷上难度虚高似乎已成常态 不过这道题相比于愚人节的那几道相对好得多,没有被评成紫题. 这道题题面意思比较清楚,就是对于给定数量的'a ...

  8. BZOJ3004: 吊灯(结论 毒瘤)

    题意 $n$个节点的树,判断能否划分成$\frac{n}{k}$个大小为$k$的联通块 Sol 首先$k$必须是$n$的倍数. 然后刚开始我就非常傻的以为输出所有约数就行了.. 但是图是这样,$k = ...

  9. python中__file__

    用__file__ 来获得脚本所在的路径,比如文件在/root下 cat tee #!/usr/bin/env pythonprint __file__              #得到相对路径tee ...

  10. Java中方法的继承以及父类未被子类覆盖的方法调用的问题

    在看java继承这一块的时候发现了一个问题,即父类未被子类覆盖的方法是如何调用的? 是子类拥有了父类的该方法只是没有显示表示,还是子类调用了父类的该方法. 为此做了一下验证 代码如下: public ...