Python中Dictionary的sort by key和sort by value(排序)

Python中的Dictionary类似于C++ STL中的Map

Sort by value

#remember to import
from operator import itemgetter
dict={.....}

#sort by value
sorted(dict.items(), key=itemgetter(1), reverse=True)

Sory by Key

#sort by key
sorted(d.items())

Leave a Reply

Your email address will not be published. Required fields are marked *