循环字典进行操作时出现:RuntimeError: dictionary changed size during iteration的解决方案
在做对员工信息增删改查这个作业时,有一个需求是通过用户输入的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
循环字典进行操作时出现:RuntimeError: dictionary changed size during iteration的解决方案的更多相关文章
- 迭代var()内置函数的时候出现RuntimeError: dictionary changed size during iteration的解决办法
下午看了Mr Seven的教学视频,其中有一段讲全局变量的视频,迭代输出全局变量的时候报错了. 视频中的做法: for k,v in vars().items(): print(k) 打印结果 for ...
- python错误之RuntimeError: dictionary changed size during iteration
pythonn报错信息: C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/Ad ...
- python 报错RuntimeError: dictionary changed size during iteration
a = {':0} for b in list(a.keys()): if a[b] == 0: del a[b] print(a) 报错是因为在字典迭代期间改变字典大小 我们可以通过取出字典的键值, ...
- Python面试题目之(针对dict或者set数据类型)边遍历 边修改 报错dictionary changed size during iteration
# result 是一个字典, 把里面属性值是None的属性删除 for key in result: if not result[key]: del result[key] continue 但是报 ...
- python : dictionary changed size during iteration
1. 错误方式 #这里初始化一个dict >>> d = {'a':1, 'b':0, 'c':1, 'd':0} #本意是遍历dict,发现元素的值是0的话,就删掉 >> ...
- Python字典的操作与使用
字典的描述 字典是一种key-value的数据类型,使用就像我们上学用的字典,通过拼音(key)来查对应字的详细内容(value). 字典的特性 1.字典是无序的(不像列表一样有下标,它通过key来获 ...
- Python—字典的操作
字典的操作: #字典的本质其实是dict类的对象 >>> a = dict([(")]) >>> a {'} 一.增加 >>> stud ...
- python中字典的操作
----------字典操作------------ --查字典1. 字典名["元素名称"]2. 字典名.get("元素名称")-获取不存在得元素名称,.get ...
- Python学习杂记_6_字典常用操作
字典操作 字典是由一对花括号括起来的一组“键值对”,每个键值对就是字典的一个元素,元素在字典中是无序的,常见操作如下: info = { 'name':'xiaoming', 'sex':'nan', ...
随机推荐
- oracle rollback 观察时间
###########issue 0: db alert 有如下提示, thread 1 cannot allocatete new log, sequenec 1111 通过检查v$log ,发现1 ...
- PostgreSQL 导出导入表中指定查询数据
创建一临时表: create table test_view as select * from test where date(to_timestamp(endtime))>='2012-09- ...
- 打war包时无法把src/main/java里的xml文件打包上去
maven打包默认打src/mian/resource里面的xml,而不会去src/main/java,所以 再pom.xml里的bulid节点里加上 <resources> <re ...
- java jstat
jstat 虚拟机统计信息监视工具: jstat (JVM Statistics Monitoring Tool) 适用于监视虚拟机各种运行状态信息的命令行工具. 命令格式: jstat [ opti ...
- java中两个map比较
一 /** * 用map的keySet()的迭代器(性能效率较低) * */ public void compareMap1 (){ Map<String, String> m1 = ne ...
- babel7中 corejs 和 corejs2 的区别
babel7中 corejs 和 corejs2 的区别 最近在给项目升级 webpack4 和 babel7,有一些改变但是变化不大.具体过程可以参考这篇文章 webpack4:连奏中的进化.只是文 ...
- CSS grid layout demo 网格布局实例
直接 上代码,里面我加注释,相当的简单, 也可以去我的github上直接下载代码,顺手给个星:https://github.com/yurizhang/micro-finance-admin-syst ...
- vuejs vue-resource post方式提交参数PHP $_POST获取不到
- sublime text 3 入门技巧与常见问题解决
1. 常见问题 - 解决sublime 窗口栏(UNREGISTERED)(未购买)导致的经常性弹窗 解决方法: 点击Help -> About Sublime Text,查看sublimete ...
- JVM的内存划分
1.栈内存:栈内存主要是用来运行函数的,在函数中定义的所有变量,都会在这个内存开辟空间. 在栈内存中定义的变量,不初始化,是不能直接使用的. 注意:所有的函数都必须在栈内存中运行. 而jvm只会运行处 ...