在python中对Dict的values进行排序,并且得到对应的key

Saturday, January 30, 2010

>>> d= {'a':'2','b':'3','c':'3','d':'4','e':'2'}
>>> sorted(d.items(),cmp=lambda x,y:cmp(x[1],y[1]),reverse=True)[:3]
[('d', '4'), ('c', '3'), ('b', '3')]
>>> for keyword in sorted(d.items(),cmp=lambda x,y:cmp(x[1],y[1]),reverse=True)[:3]:
print keyword[0]
...

This entry was tagged Python

comments powered by Disqus

© 2009-2013 lxneng.com. All rights reserved. Powered by Pyramid

go to Top