Everyone says if you push tuple into heapq
, it will take the first argument as the comparison factor.
But it's not! I'm curious what's wrong in my code?
for task_name, counter in tasks_counter.items():
heappush(tasks_q, (-int(counter), task_name, counter))
heapify(tasks_q)
while tasks_q:
print(tasks_q.pop())
output
(-1, 'G', 1)
(-1, 'F', 1)
(-1, 'E', 1)
(-1, 'D', 1)
(-1, 'C', 1)
(-1, 'B', 1)
(-6, 'A', 6)
I think I should get the item with value A
first, right?
But it's not.
Any alternative solution to use built-in priority queue with Python?