Skip to content

Latest commit

 

History

History
23 lines (17 loc) · 328 Bytes

File metadata and controls

23 lines (17 loc) · 328 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']