Does python have a built in min-heap data structure?
问题 Does python have a built in min-heap data structure in 2.7.3? I don't want to import code. I want something like myheap = minheap(key=lambda x: x[1]) myheap.add(obj) o = myheap.pop() Is this possible? 回答1: Like everybody says, heapq is it -- but, as nobody's mentioned yet, it doesn't support a key= ! So you need to fall back to the good old DSU (decorate-sort-undecorate) idiom that key= uses internally wherever it's supported (alas not in heapq , except for the functions nlargest and