@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']
python-small-examples/md/76.md at master · data-python/python-small-examples