Skip to content

Instantly share code, notes, and snippets.

@efrenfuentes
Last active June 1, 2016 18:40
Show Gist options
  • Save efrenfuentes/f328611c4c952b8b7a0a237ecee92d0e to your computer and use it in GitHub Desktop.
Save efrenfuentes/f328611c4c952b8b7a0a237ecee92d0e to your computer and use it in GitHub Desktop.
Histogram 2
#!/usr/bin/env python
import operator
import re
def histogram_for(text):
text = re.sub('[!@#$-]', '', text)
words = text.lower().split()
frequency = {}
for word in words:
if word in frequency:
frequency[word] += 1
else:
frequency[word] = 1
return dict(sorted(frequency.items(), key=operator.itemgetter(1)))
def print_histogram(histogram):
for key, value in histogram.iteritems():
print "{0}: {1}".format(key, value)
if __name__ == '__main__':
text = "I was 09809 home -- Yes! yes! You was"
print_histogram(histogram_for(text))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment