# 初始化字典
a = {"b": 5, "c": 2, "a": 4, "d": 1}
# 对字典按键(key)进行排序(默认由小到大)
test_1 = sorted(a.items(), key=lambda x: x[0])
# 输出结果
print(test_1) # [('a', 4), ('b', 5), ('c', 2), ('d', 1)]
# 对字典按值(value)进行排序(默认由小到大)
test_2 = sorted(a.items(), key=lambda x: x[1])
# 输出结果
print(test_2) # [('d', 1), ('c', 2), ('a', 4), ('b', 5)]