Coding/Python Matlab

파이썬 - how to sort a Python dict

smores 2012. 1. 26. 21:07
http://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/ 

sort by key

for key in sorted(mydict.iterkeys()):
    print "%s: %s" % (key, mydict[key])

 
sort by value

for key, value in sorted(mydict.iteritems(), key=lambda (k,v): (v,k)):
    print "%s: %s" % (key, value)