python - Iterating through a dictionary for X number of times -
assume dictionary contains more 10 key-value pairs. dictionary ought sorted values (of integers). print out top 10 values (and corresponding keys). think there better solution have given here.
for keys in sorted(x): c=c+1 if c>10: break else: print keys, x['keys']
for key in sorted(x, key=x.get, reverse=true)[:10]: print key, x[key]
for large dict
should consider using heapq
from heapq import nlargest key in nlargest(10, x, key=x.get): print key, x[key]
Comments
Post a Comment