Skip to content

Latest commit

 

History

History
26 lines (18 loc) · 399 Bytes

76.md

File metadata and controls

26 lines (18 loc) · 399 Bytes
@author jackzhenguo
@desc 
@date 2019/4/19

76 Topn 字典

返回字典d前n个最大值对应的键

from heapq import nlargest
def topn_dict(d, n):
    return nlargest(n, d, key=lambda k: d[k])

测试:

topn_dict({'a': 10, 'b': 8, 'c': 9, 'd': 10}, 3)  
# ['a', 'd', 'c']
[上一个例子](75.md) [下一个例子](77.md)