Python 排序

字典排序的原理就是把字典转化成可以迭代的list来进行排序
字典排序(复杂):

dicts = {"4":{"key1":"a","num":1},
         "2":{"key1":"a","num":2},
         "3":{"key1":"a","num":3},
         "1":{"key1":"a","num":4}}
items = sorted(dicts.items(), key=lambda d: d[1]['num'], reverse=True)

字典:最简单的按照key来

items = dicts.items() 
items.sort() 

下面来看一下sorted方法

sorted(...)
    sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
#按照key进行排序 
sorted(dict1.items(), key=lambda d: d[0]) 
#按照value进行排序 
sorted(dict1.items(), key=lambda d: d[1]) 

你可能感兴趣的:(python,sorted)