[英]RuntimeError: dictionary changed size during iteration in python
I'm trying to change all the values of F to 1 and the values of M to 0 so I can create a dummy variable and then check the importance of Gender in my predicted results. 我试图将F的所有值更改为1,将M的值更改为0,以便创建一个虚拟变量,然后检查性别在预测结果中的重要性。 I created a dictionary this way 我用这种方式创建了字典
Gender_dict = df_new.set_index("Student_ID") ["Gender"].to_dict()
print (Gender_dict)
and got: 并得到:
{366: 'F', 375: 'F', 381: 'F', 391: 'M', 399: 'M', 427: 'M', 429: 'M', 431: 'M', 435: 'M', 444: 'M', 452: 'F', 464: 'M', 472: 'F', 478: 'M', 484: 'F', 487: 'M', 495: 'M', 507: 'F', 1511: 'M', 1512: 'M', 1517: 'F', 1521: 'M', 1526: 'M', 1532: 'F', 1534: 'M', 1540: 'M', 1554: 'M', 1574: 'M', 1576: 'F', 1580: 'M', 1581: 'F', 1592: 'F', 1594: 'F', 1634: 'F', 1638: 'M', 1639: 'M', 1651: 'M', 1672: 'M', 2550: 'M', 7311: 'M', 7313: 'M', 7327: 'M', 7356: 'M', 7361: 'F', 7366: 'M', 7367: 'M', 7372: 'M', 7382: 'M', 7436: 'M', 7440: 'M', 7446: 'M', 8305: 'M', 8312: 'M', 8320: 'M', 8340: 'M', 8342: 'M', 8358: 'M', 8361: 'M', 8363: 'M', 8371: 'M', 8381: 'M', 8383: 'F', 8386: 'F', 8390: 'M', 8391: 'M', 8426: 'M', 8428: 'F', 8435: 'M', 8440: 'M', 8452: 'M', 8457: 'M', 9447: 'M', 9478: 'F', 9486: 'F', 9489: 'M', 9540: 'M', 9545: 'M', 9546: 'M'}
I thought this might work 我认为这可能有效
for Student_ID, Gender in Gender_dict.items():
if Gender == "F":
Gender_dict[Gender] = "1"
elif Gender == "M":
Gender_dict[Gender] = "0"
print (Gender_dict)
But I get this error: 但是我得到这个错误:
RuntimeError
Traceback (most recent call last)
<ipython-input-41-acce392dae9f> in <module>()
5 #a1[color] = "Tulip"
6
----> 7 for Student_ID, Gender Gender_dict.items():
8 if Gender == "F":
9 Gender_dict[Gender] = "1"
RuntimeError: dictionary changed size during iteration
I tried adapting what I found to suit my purpose but can't get it to work. 我尝试调整发现的内容以适合自己的目的,但无法使其正常工作。 I also tried just about every .replace()
and .apply()
method I could find but nothing seems to work so I thought this would work. 我还尝试了几乎所有可以找到的.replace()
和.apply()
方法,但似乎没有任何效果,因此我认为这可以工作。
Any help is greatly appreciated. 任何帮助是极大的赞赏。
It's not totally clear what you're trying to achieve, but if you're doing one-hot encoding by hand, you just have typo in key name 尚不清楚您要实现的目标,但是如果您要手动进行一键编码,则关键字名称中只会出现错字
for Student_ID, Gender in Gender_dict.items():
if Gender == "F":
Gender_dict[Student_ID] = "1"
elif Gender == "M":
Gender_dict[Student_ID] = "0"
If you create a lookup dictionary: 如果您创建查找字典:
gender_lookup = { 'F' : 1, 'M' : 0 }
Then you can update your other dictionary using a dictionary comprehension: 然后,您可以使用字典理解来更新其他字典:
updated = { student_id : gender_lookup[gender] for student_id,gender in Gender_dict.items() }
when iterating on a dictionary, it's perfectly all right to change values associated to an existing key. 在字典上进行迭代时,完全可以更改与现有键关联的值。
What you cannot do is: add or remove keys. 您不能做的是:添加或删除密钥。
You're accidentally doing this by using the value of the dictionary as the key, creating extra keys and generating the error message. 您不小心通过使用字典的值作为键,创建额外的键并生成错误消息来执行此操作。
Generally, this kind of full dict update is better done using a dictionary comprehension, overriding the old dictionary: 通常,最好使用字典理解来覆盖旧字典,以完成这种完整的dict更新 :
Gender_dict = {Student_ID:"1" if Gender == "F" else "M" for Student_ID, Gender in Gender_dict.items()}
As the exception message suggests, you can't mutate a dict while you iterate over its items. 就像异常消息所暗示的那样,在迭代字典中的项时,您不能对其进行变异。 You can iterate over a copy of the dict instead: 您可以改为遍历该字典的副本:
for Student_ID, Gender in list(Gender_dict.items()):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.