Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
topfew/tf.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
40 lines (31 sloc)
878 Bytes
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
#! /usr/bin/env python3 | |
""" | |
Topfew. | |
Usage: | |
tf [--fields=<fields>] [--few=<few>] <filename> | |
Options: | |
--fields=<fields> Which fields to select [default: 1] | |
--few=<few> How many results to display [default: 10] | |
""" | |
from docopt import docopt | |
args = docopt(__doc__, version="0.1") | |
fields = [int(x) for x in args["--fields"].split(',')] | |
few = int(args["--few"]) | |
filename = args["<filename>"] | |
keys = {} | |
with open(filename) as f: | |
for line in f.readlines(): | |
key = "" | |
tokens = line.split(' ') | |
for field in fields: | |
key = key + tokens[field-1] | |
if key not in keys: | |
keys[key] = 1 | |
else: | |
keys[key] = keys[key] +1 | |
count = 0 | |
for k,v in {k: v for k, v in sorted(keys.items(), key=lambda item: item[1], reverse=True)}.items(): | |
print(v, k) | |
count += 1 | |
if count >= few: | |
break |