Skip to content

Instantly share code, notes, and snippets.

@AndersonFirmino
Forked from andresmachado/word_count.py
Created March 16, 2016 12:46
Show Gist options
  • Save AndersonFirmino/ab981182cef669a56098 to your computer and use it in GitHub Desktop.
Save AndersonFirmino/ab981182cef669a56098 to your computer and use it in GitHub Desktop.
import sys
def count_words(text_file):
file = open(textfile,'r+')
word_list = {}
for word in file.read().split():
if word not in word_list:
word_list[word] = 1
else:
word_list[word] += 1
return word_list
textfile = sys.argv[1]
list_of_words = count_words(textfile)
for word in sorted(list_of_words, key=list_of_words.get, reverse=True):
print word, list_of_words[word]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment