I have another heapq question, that I hope I can get some clarity on.
I have a list:
rooms = [[0, 2], [0, 3], [9, 0], [0, 4], [0, 1]]
The smallest element in this list is correctly identified by nsmallest as:
nsmallest(1, rooms)[0]
output:
[0, 1]
However when I use heappop to remove the smallest element, it removes [0, 2]:
a, b = heappop(rooms)
print([a, b])
output:
[0, 2]
However if I sort rooms first, it will pop the correct element. Why do I need to sort rooms?
A heap (according to
heapq) is a list that satisfies the heap propertynsmallesttakes an arbitrary iterable as an argument, and constructs a heap in order to produce its result.heappoptakes a pre-made heap as an argument and modifies it, preserving the heap property in the process.In your example,
roomsis not a heap, becauserooms[1] == [0, 3]is not less thanrooms[2*1 + 2] == rooms[4] == [0, 1], soheappop(rooms)is not guaranteed to work.