Python按value取字典中的top3

Posted eric20180814

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python按value取字典中的top3相关的知识,希望对你有一定的参考价值。

n = 3
data = {John: 100, Erica: 40, Make:65, Tom: 80}
print(data) temp_list
= sorted(data.items(), key= lambda item:item[1], reverse= True) top3_list = temp_list[: n] print(top3_list) top3_dict = {} for l in top3_list: top3_dict[l[0]] = l[1] print(top3_dict)

结果为:

{John: 100, Erica: 40, Make: 65, Tom: 80}
[(John, 100), (Tom, 80), (Make, 65)]
{John: 100, Tom: 80, Make: 65}

参考自:https://blog.csdn.net/u014662865/article/details/81807112

 

以上是关于Python按value取字典中的top3的主要内容,如果未能解决你的问题,请参考以下文章