Skip to content

Instantly share code, notes, and snippets.

@SpotlightKid
Last active April 19, 2024 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpotlightKid/9b9032c918ba4334d69a692253858aa1 to your computer and use it in GitHub Desktop.
Save SpotlightKid/9b9032c918ba4334d69a692253858aa1 to your computer and use it in GitHub Desktop.
Count occurences of same lines read from standard input.
"""Count occurences of same lines read from standard input.
Outputs a table sorted by descending count.
"""
import sys
from collections import Counter
import natsort
import tabulate
counts = Counter(sys.stdin.read().strip().splitlines())
# sort by item
data = natsort.humansorted(counts.items())
# sort by descending count first
data.sort(reverse=True, key=lambda x: x[1])
print(tabulate.tabulate(data, headers=["Item", "Count"], tablefmt="grid"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment