【发布时间】:2018-10-30 10:23:38
【问题描述】:
我有一本带有{key: count} 的字典,比如说
status_count = {'MANAGEMENT ANALYSTS': 13859, 'COMPUTER PROGRAMMERS': 72112}
我正在尝试为 heapq.nlargest() 编写一个基于计数排序的键函数,如果有关系,我必须根据键的字母顺序(a-z)进行排序。我必须使用 heapq.nlargest 因为非常大的 N 和小的 k = 10。
这是我到现在得到的,
top_k_results = heapq.nlargest(args.top_k, status_count.items(), key=lambda item: (item[1], item[0]))
但是,如果按字母顺序打破联系,这将是不正确的。请帮忙!
【问题讨论】:
-
你能展示你正在编写的字典的样本吗?
-
@Austin 我刚刚更新了问题的详细信息。
标签: python python-3.x lambda heap python-collections