0

I have lists :

mylist = [[3, "A", "X", "xyz", 0.93243],[43, "C", "X", "zyx", 0.23243],[13, "B", "X", "xyz", 0.43243]]

heapq.heapify(mylist)
mylist
[[3, 'A', 'X', 'xyz', 0.93243],
 [43, 'C', 'X', 'zyx', 0.23243],
 [13, 'B', 'X', 'xyz', 0.43243]]

When I want to pop list by using heapq.heappop(mylist). First item that will be popped is [3, 'A', 'X', 'xyz', 0.93243]. I guess, it is because the first value (3) in the item list is the lowest.

Is it possible If I want to use the last value, index[4] as the basis. So the popping item is based on the last element in the list?

Expected output when I want to pop item, the first item will be popped is [43, 'C', 'X', 'zyx', 0.23243] because the index[4] has lowest value which is 0.23243

user46543
  • 1,033
  • 4
  • 13
  • 23

1 Answers1

1

Your lists need to be objects that implement a custom cmp function like so:

class MyObject():
...     
...     def __cmp__(self, other):
...         return cmp(self.val, other.val)
...
Vj-
  • 722
  • 6
  • 18