Skip to content

Instantly share code, notes, and snippets.

@natbusa
Created March 19, 2014 14:40
Show Gist options
  • Save natbusa/9643054 to your computer and use it in GitHub Desktop.
Save natbusa/9643054 to your computer and use it in GitHub Desktop.
word count: python
#readin and filter
txt = [c for c in open('lorem.txt').read().lower() if c.isalpha() or c==' ']
#groupBy in a dictionary
wc = dict()
for w in ''.join(txt).split():
wc[w] = wc.setdefault(w, 0) + 1
#output
for k,v in wc.iteritems():
print('{1} {0}'.format(k,v))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment