RuntimeError: dictionary changed size during iteration

▼魔方 西西 提交于 2020-01-07 05:46:25

问题


This is my code:

import os
import collections
def make_dictionary(train_dir):
    emails=[os.path.join(train_dir,f) for f in os.listdir(train_dir)]
    all_words=[]
    for mail in emails:
        with open(mail) as m:
            for i,line in enumerate(m):
                if i==2: #Body of email is only 3rd line of text file 
                    words=line.split()
                    all_words+=words
    dictionary=collections.Counter(all_words)
    # Paste code for non-word removal here(code snippet is given below)
    list_to_remove=dictionary.keys()
    for item in list_to_remove:
        if item.isalpha()==False:
            del dictionary[item]
        elif len(item)==1:
            del dictionary[item]
    dictionary=dictionary.mostcommon[3000]
    print (dictionary)

make_dictionary('G:\Engineering\Projects\Python\Documents\enron1\ham')

I am receiving the error "RuntimeError: dictionary changed size during iteration" on writing this code. I have only text files in the directory. Any help will be appreciated.


回答1:


Take a look at this two code snippets:

d = {1: 1, 2: 2}
f = [x for x in d]
del d[1]
print(f)  # [1, 2]

and:

d = {1: 1, 2: 2}
f = d.keys()
del d[1]
print(f)  # dict_keys([2])

As you can see, in the first one the dictionary d and the list f are not related to one another; changes in the dict are not reflected to the list.

On the second snippet, due to the way we create the list f it remains linked to the dict so deleting elements of the dict also removes them from the list.

Both behaviors might be somewhere helpful but in your scenario it is the first one you want.



来源:https://stackoverflow.com/questions/45062623/runtimeerror-dictionary-changed-size-during-iteration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!