Skip to content

Instantly share code, notes, and snippets.

@d2fn
Last active August 29, 2015 14:17
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 d2fn/71c73960192d03eb3ffe to your computer and use it in GitHub Desktop.
Save d2fn/71c73960192d03eb3ffe to your computer and use it in GitHub Desktop.
Bucketed top-k in Python using heap-ordered arrays
#!/usr/bin/env python
"""
Usage: topk [key-column-name] [score-column-name]
Reads tsv rows, including a header row, from stdin.
Writes to stdout the top 10 rows per distinct value of the given key column.
Author Dietrich Featherston
"""
import sys
import re
import heapq
import string
K = 10
# push the row into the heap ordered list for the given bucket
# using a heap of bounded size keeps memory use per bucket bound
# and limits time complexity of checking N rows to O(NlogK)
def add(buckets, key, score, row):
h = []
if key in buckets:
h = buckets[key]
else:
buckets[key] = h
heapq.heappush(h, (score, row))
if(len(h) > K):
heapq.heappop(h)
def tsv(row):
return string.join(row, '\t')
if __name__ == "__main__":
buckets = {}
columns = []
first = True
key_column = sys.argv[1]
score_column = sys.argv[2]
key_column_index = 0
score_column_index = 0
# read each line
for line in sys.stdin:
row = re.split("\s+", line)
# first row is column names
if first:
columns = row
key_column_index = columns.index(key_column)
score_column_index = columns.index(score_column)
first = False
else:
# compute the row key and score
key = int(row[key_column_index])
score = int(row[score_column_index])
# add to heap
add(buckets, key, score, row)
print tsv(columns)
# sort the keys
keys = sorted(buckets, key=lambda key: buckets[key])
for key in keys:
# get the heap for this key
h = buckets[key]
ordered_list = reversed([heapq.heappop(h) for i in range(len(h))])
# print each item in the heap in order
for (score, row) in ordered_list:
print tsv(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment