def sort_dict_by_value(d, reverse = False):
  return dict(sorted(d.items(), key = lambda x: x[1], reverse = reverse))
d
= {'one': 1, 'three': 3, 'five': 5, 'two': 2, 'four': 4} sort_dict_by_value(d) # {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5} sort_dict_by_value(d, True) # {'five': 5, 'four': 4, 'three': 3, 'two': 2, 'one': 1}

 

相关文章:

  • 2021-09-18
  • 2022-12-23
  • 2022-12-23
  • 2021-09-25
  • 2022-01-17
  • 2021-06-09
  • 2022-03-03
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-28
  • 2021-06-21
  • 2022-02-01
  • 2022-12-23
  • 2022-12-23
  • 2021-06-16
相关资源
相似解决方案