#对字典排序
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)]
#对字典排序
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)]
本文标题:【python-列表/字典处理】对字典排序
本文链接:https://www.haomeiwen.com/subject/lkzqqhtx.html
网友评论