Last active
June 1, 2016 18:40
-
-
Save efrenfuentes/f328611c4c952b8b7a0a237ecee92d0e to your computer and use it in GitHub Desktop.
Histogram 2
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
#!/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