Essentially I am looking for an efficient way to implement custom comparators using heapq
.
For instance x = [('a',5),('c',3),('d',2),('e',1)]
I could heapify it heapq.heapify(x)
then pop the min value heapq.heappop(x)
which would return ('a', 5)
. How can I make it such that it returns in reverse lexicographical order ('e', 1)
?
I know the convention when it comes to numbers is simply multiply the first element of the tuple by -1
. Are there simple tricks like that when it comes to strings? I know I could potentially implement a map from a to z ... z to a but it sounds cumbersome.