sorting - Sort dict by second value in tuple and return dict -
from collections import defaultdict,ordereddict tt=defaultdict (list) tt={'abcd':(23,77),'ddef':(55,22)} c=ordereddict (sorted (tt.items (),key=lambda t: t[1][1])) print (c) d=list ((k,v) k,v in c.items()) print (d)
this sorts correctly c.
list makes dict ordereddict
d gets {'ddef':(55,22),'abcd':(23,77)}
modified code. works fine now.
need display top (n) keys in sorted order second element of tuple.
dict seems best route, there ever 50 items in list,i need top ten in order.
Comments
Post a Comment