sorted多条件排序

l=[{'a':1,'b':3,'c':'c'},{'a':2,'b':3,'c':'c'},{'a':1,'b':2,'c':'a'},{'a':3,'b':4,'c':'s'}]
sorted(l,key=lambda x:x['b'])  # 根据单个条件排序
[{'a': 1, 'b': 2, 'c': 'a'}, {'a': 1, 'b': 3, 'c': 'c'}, {'a': 2, 'b': 3, 'c': 'c'}, {'a': 3, 'b': 4, 'c': 's'}]
sorted(l,key=lambda x:(x['b'],x['c']))  # 根据多个条件排序
[{'a': 1, 'b': 2, 'c': 'a'}, {'a': 1, 'b': 3, 'c': 'c'}, {'a': 2, 'b': 3, 'c': 'c'}, {'a': 3, 'b': 4, 'c': 's'}]
原文地址:https://www.cnblogs.com/wjlv/p/14688045.html