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

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -