Skip to content

Instantly share code, notes, and snippets.

@Natim
Created February 14, 2014 15:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Natim/9003046 to your computer and use it in GitHub Desktop.
Save Natim/9003046 to your computer and use it in GitHub Desktop.
Faster count uniq cut file (for flake8 or log for instance) (Thx @boblefrag for http://snippet.gabory.fr/snippet/9/)
# From http://snippet.gabory.fr/snippet/9/
# Similar to `cut -d ":" -f 1 | sort | uniq -c | sort -n`
#
# Usage: flake8 venv | python cutfdu.py
# Mock : flake8 venv | cut -d ":" -f 1 | sort | uniq -c | sort -n
#
# From Natim with love (2014-02-14)
import itertools
import sys
print "\n".join([" %s\t%s" % (elem[1], elem[0]) for elem in sorted(
[(test[0], len(
[a for a in test[1]]
)
) for test in itertools.groupby(
sorted(
[
line.split(":")[0] for line in
sys.stdin.readlines()
]
),
lambda x: x)
],
lambda a, b: a[1] - b[1]
)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment