Skip to content

Instantly share code, notes, and snippets.

@astrofrog
Created November 21, 2010 02:36
Show Gist options
  • Save astrofrog/708383 to your computer and use it in GitHub Desktop.
Save astrofrog/708383 to your computer and use it in GitHub Desktop.
Word count stats
# Pipe text into this to get a word count list in decreasing order, e.g.:
#
# $ detex ms.tex | python wordcount.py
# model:9
# star:8
# formation:7
# YSOs:7
# ...
import sys
text = sys.stdin.read()
text = text.replace('\n',' ')
words = text.split()
count = {}
for word in words:
if word in count:
count[word] += 1
else:
count[word] = 1
values = list(set(count.values()))
values.sort()
values = values[::-1]
for value in values:
for word in count:
if count[word] == value:
print "%s:%i" % (word, count[word])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment