Note that there are some explanatory texts on larger screens.

plurals
  1. POWeird output from .itemgetter for list sorting by values python
    primarykey
    data
    text
    <p>So I'm working on the <a href="https://developers.google.com/edu/python/" rel="nofollow noreferrer">Google Python Code Class</a> and attempting to do the Word_Count.py exercise. The aim is to create a dictionary of words (key) sorted by word count (value) and return them as tuples for printing.</p> <p>I've created a helper function to create my dictionary:</p> <pre><code>def dict_creator(filename): #helper function to create a dictionary each 'word' is a key and the 'wordcount' is the value input_file = open(filename, 'r') #open file as read for line in input_file: #for each line of text in the input file words = line.split() #split each line into individual words for word in words: #for each word in the words list(?) word = word.lower() #make each word lower case. if word not in word_count: #if the word hasn't been seen before word_count[word] = 1 #create a dictionary key with the 'word' and assign a value of 1 else: word_count[word] += 1 #if 'word' seen before, increase value by 1 return word_count #return word_count dictionary word_count.close() </code></pre> <p>I'm now in the process of creating the dictionary sorted by value (largest to smallest) using the .itemgetter method outlined in this post here: <a href="https://stackoverflow.com/a/1915602/1842478">link</a>. Here is my code:</p> <pre><code>def print_words(filename): word_count = dict_creator(filename) #run dict_creator on input file (creating dictionary) print sorted(word_count.iteritems(), key=operator.itemgetter(1), reverse=True) #print dictionary in total sorted descending by value. Values have been doubled compared to original dictionary? for word in sorted(word_count.iteritems(), key=operator.itemgetter(1), reverse=True): #create sorted list of tuples using operator module functions sorted in an inverse manner a = word b = word_count[word] print a, b #print key and value </code></pre> <p>However, when I run the code on the test file and also on a smaller file it throws a key error (shown below).</p> <pre><code>Traceback (most recent call last): File "F:\Misc\google-python-exercises\basic\wordcount_edited.py", line 74, in &lt;module&gt; print_words(lorem_ipsum) #run input file through print_words File "F:\Misc\google-python-exercises\basic\wordcount_edited.py", line 70, in print_words b = word_count[word] KeyError: ('in', 3) </code></pre> <p>I've printed the original dictionary and the sorted dictionary and it appears all the values have been doubled when the dictionary has been sorted. I've looked through several threads related to this sort of issue and checked the .itemgetter documentation, however I can't seem to find anyone else with a similar issue.</p> <p>Could anyone point out what's causing my code to iterate over the dictionary a second time in the word_count function which causes the increase in the values?</p> <p>Thanks!</p> <p>SB</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload