Trying to output the x most common words in a text file
<p>I'm trying to write a program that will read in a text file and output a list of most common words (30 as the code is written now) along with their counts. so something like:</p>

<pre><code>word1 count1
word2 count2
word3 count3
...   ...
...   ...
wordn countn
</code></pre>

<p>in order of count1 > count2 > count3 >... >countn. This is what I have so far but I cannot get the sorted function to perform what I want. The error I get now is:</p>

<pre><code>TypeError: list indices must be integers, not tuple
</code></pre>

<p>I'm new to python. Any help would be appreciated. Thank you.</p>

<pre><code> def count_func(dictionary_list):
  return dictionary_list[1]

def print_top(filename):
  word_list = {}
  with open(filename, 'r') as input_file:

    count = 0

    #best
    for line in input_file:
      for word in line.split():
        word = word.lower()
        if word not in word_list:
          word_list[word] = 1
        else:
          word_list[word] += 1

#sorted_x = sorted(word_list.items(), key=operator.itemgetter(1))
#  items = sorted(word_count.items(), key=get_count, reverse=True)

  word_list = sorted(word_list.items(), key=lambda x: x[1])

  for word in word_list:
    if (count &gt; 30):#19
      break
    print "%s: %s" % (word, word_list[word])
    count += 1


# This basic command line argument parsing code is provided and
# calls the print_words() and print_top() functions which you must define.
def main():
  if len(sys.argv) != 3:
    print 'usage: ./wordcount.py {--count | --topcount} file'
    sys.exit(1)

  option = sys.argv[1]
  filename = sys.argv[2]
  if option == '--count':
    print_words(filename)
  elif option == '--topcount':
    print_top(filename)
  else:
    print 'unknown option: ' + option
    sys.exit(1)

if __name__ == '__main__':
  main()
</code></pre>

<p>Use the <a href="https://docs.python.org/3/library/collections.html#collections.Counter" rel="nofollow"><code>collections.Counter</code></a> class.</p>

<pre><code>from collections import Counter

for word, count in Counter(words).most_common(30):
    print(word, count)
</code></pre>

<p>Some unsolicited advice: Don't make so many functions until everything is working as one big block of code. Refactor into functions <em>after</em> it works. You don't even need a main section for a script this small.</p>

python sorting dictionary tuples sorted