Skip to content

Instantly share code, notes, and snippets.

@qodeninja
Created May 29, 2015 16:04
Show Gist options
  • Save qodeninja/7ca09823dee5e8dc839f to your computer and use it in GitHub Desktop.
Save qodeninja/7ca09823dee5e8dc839f to your computer and use it in GitHub Desktop.
Python - simple words in a file histogram (sorted by count)
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