Skip to content

Instantly share code, notes, and snippets.

@jmora
Created May 30, 2012 11:08
Show Gist options
  • Save jmora/2835547 to your computer and use it in GitHub Desktop.
Save jmora/2835547 to your computer and use it in GitHub Desktop.
Short script to count how many times words appear on a file, you may need to change the re
#!/usr/bin/python3
import re, sys
if __name__ == "__main__":
c = {}
with open(sys.argv[1], "r") as f:
for l in f:
for e in re.findall("\w+", l):
c[e] = c.get(e, 0) + 1
for e in sorted(c.items(), key=lambda i:i[1], reverse=True):
print("%s %d"%e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment