Last active
April 19, 2024 20:50
-
-
Save SpotlightKid/9b9032c918ba4334d69a692253858aa1 to your computer and use it in GitHub Desktop.
Count occurences of same lines read from standard input.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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