Python Forum
RuntimeError: dictionary changed size during iteration
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RuntimeError: dictionary changed size during iteration
#1
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] += 1               
I 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] += 1
Yet I am getting the same error:
Error:
RuntimeError: dictionary changed size during iteration
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.
Reply
#2
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)
Output:
defaultdict(<class 'int'>, {'word': 4, 'word2': 3, 'word3': 1, 'word5': 2, 'word1': 2})
Reply
#3
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)]
Reply
#4
it would be helpful to show the full traceback you get (in error tags)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help RuntimeError: no running event loop marpaslight 5 3,623 Oct-18-2022, 10:04 PM
Last Post: marpaslight
  The behavior of tune model has changed Led_Zeppelin 5 4,360 Oct-21-2021, 06:52 PM
Last Post: jefsummers
  bleak library RuntimeError: This event loop is already running alice93 3 4,028 Sep-30-2021, 08:06 AM
Last Post: alice93
  how can a variable change if I haven't changed it? niminim 5 2,992 Apr-07-2021, 06:57 PM
Last Post: niminim
  RuntimeError: generator raised StopIteration quest 1 5,754 Mar-28-2021, 08:11 PM
Last Post: quest
  new help with dictionary and dataframe iteration AlphFinan 0 1,497 Oct-13-2020, 11:04 PM
Last Post: AlphFinan
  RuntimeError: This event loop is already running newbie2019 2 6,906 Sep-30-2020, 06:59 PM
Last Post: forest44
  RuntimeError: Optimal parameters not found: Number of calls to function has reached m bntayfur 0 6,073 Aug-05-2020, 04:41 PM
Last Post: bntayfur
  Dictionary iteration and creation a new dictionary from duplicates xrsxlnx 2 2,089 Mar-30-2020, 10:43 AM
Last Post: xrsxlnx
  size of set vs size of dict zweb 0 2,119 Oct-11-2019, 01:32 AM
Last Post: zweb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020