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>Using <code>itertools</code>' <code>groupby</code>:</p>

<pre class="lang-py prettyprint-override"><code>from itertools import groupby

words = sorted([w.lower() for w in open("/path/to/file").read().split()])
count = [[item[0], len(list(item[1]))] for item in groupby(words)]
count.sort(key=lambda x: x[1], reverse = True)
for item in count[:5]:
    print(*item)
</code></pre>

<ul>
<li><p>This will list the file's words, sort them and list unique words and their occurrence. Subsequently, the found list is sorted <em>by</em> occurrence by:</p>

<pre><code>count.sort(key=lambda x: x[1], reverse = True)
</code></pre></li>
<li><p>The <code>reverse = True</code> is to list the most common words first.</p></li>
<li><p>In the line:</p>

<pre><code>for item in count[:5]:
</code></pre>

<p><code>[:5]</code> defines the number of most occurring words to show.</p></li>
</ul>

python sorting dictionary tuples sorted