Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 28 lines (22 sloc) 703 Bytes
#!/usr/bin/env python
import sys
# maps words to their counts
word2count = {}
for line in sys.stdin:
# remove leading and trailing whitespace
line = line.strip()
# parse the input we got from
word, count = line.split('\t', 1)
# convert count (currently a string) to int
count = int(count)
word2count[word] = word2count.get(word, 0) + count
except ValueError:
# count was not a number, so silently
# ignore/discard this line
# sort the words by count
sorted_word2count = word2count.items()
sorted_word2count.sort(lambda x,y : y[1] - x[1])
for word, count in sorted_word2count:
print '%s\t%s'% (word, count)
Something went wrong with that request. Please try again.