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
  RuntimeError: threads can only be started once Raysz 10 6,865 Nov-24-2024, 03:24 PM
Last Post: Raysz
  Comparing two Pandas df’s and returning only changed records hobbycoder 1 1,216 Oct-29-2024, 01:55 PM
Last Post: deanhystad
  help RuntimeError: no running event loop marpaslight 5 11,984 Oct-18-2022, 10:04 PM
Last Post: marpaslight
  The behavior of tune model has changed Led_Zeppelin 5 7,121 Oct-21-2021, 06:52 PM
Last Post: jefsummers
  bleak library RuntimeError: This event loop is already running alice93 3 10,215 Sep-30-2021, 08:06 AM
Last Post: alice93
  how can a variable change if I haven't changed it? niminim 5 5,793 Apr-07-2021, 06:57 PM
Last Post: niminim
  RuntimeError: generator raised StopIteration quest 1 8,099 Mar-28-2021, 08:11 PM
Last Post: quest
  new help with dictionary and dataframe iteration AlphFinan 0 2,231 Oct-13-2020, 11:04 PM
Last Post: AlphFinan
  RuntimeError: This event loop is already running newbie2019 2 8,744 Sep-30-2020, 06:59 PM
Last Post: forest44
  RuntimeError: Optimal parameters not found: Number of calls to function has reached m bntayfur 0 8,319 Aug-05-2020, 04:41 PM
Last Post: bntayfur

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.