Created
May 29, 2015 16:04
-
-
Save qodeninja/7ca09823dee5e8dc839f to your computer and use it in GitHub Desktop.
Python - simple words in a file histogram (sorted by count)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def load_data_file(): | |
with open('data.in') as f: | |
for line in f: | |
words = line.split() | |
for word in words: | |
if histogram[word] is not None: | |
histogram[word]+=1 | |
print "Already found ({}) {}".format(word,histogram[word]) | |
else: | |
histogram[word]=0 | |
gramkeys = histogram.keys() | |
gramkeys = sorted(histogram.iteritems(), key=lambda (k,v): (v,k)) | |
for gram in gramkeys: | |
print gram[0],gram[1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment