我有一个 class person,它有三个自定义比较函数(compare_by_name / age / height),我需要三个 priorityQueue(或 Heapq)来使用不同的比较函数分别保存对象?我该怎么办。
from queue import PriorityQueue
class Person(object):
def __init__(self, name, age, height):
self.name = name
self.age = age
self.height = height
def compare_by_name(self):
# todo
def compare_by_age(self):
# todo
def compare_by_height(self):
# todo
person1 = Person("Bob", 22, 1.77)
person2 = Person("Ana", 25, 1.70)
person3 = Person("Ceb", 35, 1.88)
q1 = PriorityQueue()
# todo save 3 person and sorted by name
q2 = PriorityQueue()
# todo save 3 person and sorted by age
q3 = PriorityQueue()
# todo save 3 person and sorted by height
我试过heapq.heappush,但是好像不能保存原始对象,或者最后会按对象默认比较器排序。 并且使用数据结构列表是不可接受的,因为我需要经常插入和弹出元素。