RuntimeError: dictionary changed size during iteration - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: RuntimeError: dictionary changed size during iteration (/thread-18801.html) |
RuntimeError: dictionary changed size during iteration - Shreeniket987 - Jun-01-2019 So basically what I want is python code that counts how many times a word occurs in a paragraph for text auto summarization. I have managed to spit up the paragraph into sentences and sentences into words... So its a list of lists. And I am using the 'defaultdict" from the 'collections' library. Here is my code: from collections import defaultdict freq = defaultdict(int) for sentence in word_sent: #word_sent is my list of lists that I mentioned above. for word in sentence: freq[word] += 1I even tried like this:(Without using defaultdict) freq = {} for sentence in word_sent: #word_sent is my list of lists that I mentioned above. for word in sentence: if word not in freq.keys(): freq[word] = 1 else: freq[word] += 1Yet I am getting the same error: And this is the only dictionary I have in my entire code.Someone please help... Thank you! P.S. This is my first post so please tell me if it is comprehensible or not. RE: RuntimeError: dictionary changed size during iteration - Yoriz - Jun-01-2019 I am not getting the error that you did, you didn't give a sample of word_sent so i made my own.You should give a proper sample of word_sent so it can be tested in the same condition.from collections import defaultdict word_sent = [ ["word", "word2", "word3", "word"], ["word5", "word1", "word1", "word"], ["word5", "word2", "word2", "word"], ] freq = defaultdict(int) for sentence in word_sent: # word_sent is my list of lists that I mentioned above. for word in sentence: freq[word] += 1 print(freq)
RE: RuntimeError: dictionary changed size during iteration - snippsat - Jun-01-2019 I also don't get error from your first code,can also use Counter from collection which as the name hint is made for counting stuff.Has also useful method as most_common() .Test using word_sent that @Yoriz made. >>> from collections import Counter >>> >>> c = Counter(x for xs in word_sent for x in xs) >>> c Counter({'word': 4, 'word2': 3, 'word5': 2, 'word1': 2, 'word3': 1}) >>> c.most_common(2) [('word', 4), ('word2', 3)] >>> c.most_common(1) [('word', 4)] RE: RuntimeError: dictionary changed size during iteration - buran - Jun-01-2019 it would be helpful to show the full traceback you get (in error tags) |