python dict字典,按照Value升序, key降序
按Value升序,按key降序
例子
dicts = {1:5, 2:4, 3:8, 4:9, 5:10, 6:5, 7:5}
sort_dicts = dict(sorted(dicts.items(), key = lambda x:[x[1],-x[0]]))
print(sort_dicts)
OutPUT:{2: 4, 7: 5, 6: 5, 1: 5, 3: 8, 4: 9, 5: 10}
按Value升序
dicts = {1:5, 2:4, 3:8, 4:9, 5:10, 6:5, 7:5}
sort_dicts = dict(sorted(dicts.items(), key = lambda x:[x[1]]))
print(sort_dicts)
Output:{2: 4, 1: 5, 6: 5, 7: 5, 3: 8, 4: 9, 5: 10}
按Value降序
dicts = {1:5, 2:4, 3:8, 4:9, 5:10, 6:5, 7:5}
sort_dicts = dict(sorted(dicts.items(), key = lambda x:[x[1]], reverse = True))
print(sort_dicts)
Output:{5: 10, 4: 9, 3: 8, 1: 5, 6: 5, 7: 5, 2: 4}

浙公网安备 33010602011771号