Skip to content

Instantly share code, notes, and snippets.

@inky
Created February 14, 2011 12:06
Show Gist options
  • Save inky/825782 to your computer and use it in GitHub Desktop.
Save inky/825782 to your computer and use it in GitHub Desktop.
Count the hashtags in stdin (tested with a .txt dump of my tweets from tweetbackup.com)
#!/usr/bin/env python
import sys
from collections import defaultdict
def hashtags():
tags = [w for w in sys.stdin.read().split() if w.startswith('#')]
freq = defaultdict(int)
for tag in tags:
freq[tag] += 1
return sorted(((v, k) for (k, v) in freq.items()), reverse=True)
if __name__ == '__main__':
print '\n'.join('%7d %s' % t for t in hashtags())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment