001、
>>> a = {"b":500, "c":300, "a":700, "d":888} ## 测试字典 >>> a {'b': 500, 'c': 300, 'a': 700, 'd': 888} >>> sorted(a.items(), key = lambda x: x[0]) ## 按照字典的键进行排序 [('a', 700), ('b', 500), ('c', 300), ('d', 888)] >>> sorted(a.items(), key = lambda x: x[1]) ## 按照字典的值进行排序 [('c', 300), ('b', 500), ('a', 700), ('d', 888)]
>>> sorted(a.items(), key = lambda x: x[0], reverse = True) ## 按照字典的键逆序排序 [('d', 888), ('c', 300), ('b', 500), ('a', 700)] >>> sorted(a.items(), key = lambda x: x[1], reverse = True) ## 按照字典的值逆序排序 [('d', 888), ('a', 700), ('b', 500), ('c', 300)]