通过python的值排序 [英] sort dict by value python
本文介绍了通过python的值排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个dict。
Assume that I have a dict.
data = {1:'b', 2:'a'}
我想通过'b'和'a'对数据进行排序,所以我得到结果
And I want to sort data by 'b' and 'a' so I get the result
'a','b'
我该怎么做?
任何想法?
How do I do that?
Any ideas?
推荐答案
值使用
sorted(data.values())
要获取匹配的键,请使用键
功能
To get the matching keys, use a key
function
sorted(data, key=data.get)
获取按值排序的元组列表
To get a list of tuples ordered by value
sorted(data.items(), key=lambda x:x[1])
这篇关于通过python的值排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文