Skip to content

Instantly share code, notes, and snippets.

@sirpengi
Created December 14, 2012 04:52
Show Gist options
  • Save sirpengi/4282763 to your computer and use it in GitHub Desktop.
Save sirpengi/4282763 to your computer and use it in GitHub Desktop.
Filter top N elements of dict, looking for a more pythonic | performant | cleaner way to do this
from operator import itemgetter
def dict_top_n_items(d, n):
"""Take dictionary of form {key: count} and return a list
with only the top n entries (based on count)
If there is a tie for n-th place, include all keys that
meet the tie (thus, return list could have more than n items)
"""
if n == 0:
return []
items = sorted(d.items(), key=itemgetter(1), reverse=True)
if len(items) <= n:
return items
last = items[n-1][1]
for i, item in enumerate(items[n:]):
if item[1] != last:
return items[:n+i]
return items
if __name__ == '__main__':
d = {'a':5, 'b':4, 'c':3, 'd':3, 'e':2, 'f':2, 'g':1, 'h':0}
for x in xrange(10):
print "top", x, ":", dict_top_n_items(d, x)
top 0 : []
top 1 : [('a', 5)]
top 2 : [('a', 5), ('b', 4)]
top 3 : [('a', 5), ('b', 4), ('c', 3), ('d', 3)]
top 4 : [('a', 5), ('b', 4), ('c', 3), ('d', 3)]
top 5 : [('a', 5), ('b', 4), ('c', 3), ('d', 3), ('e', 2), ('f', 2)]
top 6 : [('a', 5), ('b', 4), ('c', 3), ('d', 3), ('e', 2), ('f', 2)]
top 7 : [('a', 5), ('b', 4), ('c', 3), ('d', 3), ('e', 2), ('f', 2), ('g', 1)]
top 8 : [('a', 5), ('b', 4), ('c', 3), ('d', 3), ('e', 2), ('f', 2), ('g', 1), ('h', 0)]
top 9 : [('a', 5), ('b', 4), ('c', 3), ('d', 3), ('e', 2), ('f', 2), ('g', 1), ('h', 0)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment