【python-列表/字典处理】对字典排序
2020-02-21 本文已影响0人
SS_c2e5
#对字典排序
dic = {0: 1, 2: 2, 4: 4, 6: 1, 7: 1, -6: 1}
dic_after = sorted(dic.items(), key=lambda x:x[1])
# 如果想按key来排序则sorted(dic.items(), key=lambda x:x[0])
# dic_after为一个列表: [(0, 1), (6, 1), (7, 1), (-6, 1), (2, 2), (4, 4)]