How to sort keys of dict by values?

前端 未结 5 1717
忘掉有多难
忘掉有多难 2021-01-19 11:13

I have a dict {\'a\': 2, \'b\': 0, \'c\': 1}.

Need to sort keys by values so that I can get a list [\'b\', \'c\', \'a\']

Is there a

5条回答
  •  天涯浪人
    2021-01-19 12:05

    >>> d={'a': 2, 'b': 0, 'c': 1}
    >>> [i[0] for i in sorted(d.items(), key=lambda x:x[1])]
    ['b', 'c', 'a']
    

提交回复
热议问题