Skip to content

Instantly share code, notes, and snippets.

@techvet
Created November 13, 2014 22:25
Show Gist options
  • Select an option

  • Save techvet/47c7e98381a4a14c95e0 to your computer and use it in GitHub Desktop.

Select an option

Save techvet/47c7e98381a4a14c95e0 to your computer and use it in GitHub Desktop.
Lamdba shortcut
import string
file_in = open('alice_in_wonderland.dat', 'r')
empty_list = []
read_file = file_in.read()
normalized_string = read_file.translate(None, string.punctuation).lower().replace("\n", " ")
story_words = normalized_string.split()
unique_words = set(story_words)
for word in unique_words:
word_count = story_words.count(word)
empty_list.append((word, word_count))
endlist = sorted(empty_list, key=lambda x: x[1], reverse=True)
final_list = endlist[:20]
asterisk_count = 20
for word in final_list:
print "|Word:|", word[0] + ' |Frequency:| ', '*' * asterisk_count
asterisk_count -= 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment