在字典中默认按照key排序,
我们可以根据自己的需求对key和value进行降序和升序排列。
利用sorted方法,根据定于:
sorted(iterable, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.对key排序时:
dict= sorted(dict.items(), key=lambda d:d[0], reverse = True)
reverse=True为降序。
利用lamda d对排序参数进行设置。
得到的结果为一元组。
对value排序时:
dict= sorted(dict.items(), key=lambda d:d[1], reverse = True)