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)
堆。有沒有簡單的方法來完成這個基於我在這裏的結構?
感謝。