0
最终,我想要的是我要做的是根据他们的分数返回前十名'项目'的列表。我试图实现使用heapq各种各样的优先级队列,到目前为止,我已经得到了什么:根据元组中的第一个值使用Python的heapq.nlargest()检索值
class my_queue:
# heap-based priority queue for top items
def __init__(self):
self.top_items = []
def push_item(self, item):
score = item.get_score()
item_name = item.get_name()
heapq.heappush(self.top_items, (score, item_name))
def top_ten(self):
top_ten_items = heapq.nlargest(10, self.top_items, key=lambda s: s[0])
print top_ten_items
我正在与key=lambda s: s[0]
做的是试图理清基于score
从(score, item_name)
堆。有没有简单的方法来完成这个基于我在这里的结构?
感谢。