Home > Software engineering >  In Python, how do I make a Priority Queue pop off multiple values of an object?
In Python, how do I make a Priority Queue pop off multiple values of an object?

Time:02-18

I have a priority queue of class objects. In my class definition I have:

def __lt__(self, other):
    return self.fn < other.fn

In the event where there are multiple objects that have the same fn value, how do I incorporate a 'tie breaker'? In other words, something like this:

def __lt__(self, other):
    return self.fn < other.fn and self.gn < other.gn

CodePudding user response:

You can add an explicit check for equality between the two fn fields, only checking gn if they're equal:

def __lt__(self, other):
    if self.fn != other.fn:
        return self.fn < other.fn
    return self.gn < other.gn
  • Related