如何使用对象比较函数反转 heapq 堆中元素的顺序?
How to invert the order of elements in a heapq heap with object comparison functions?
首先,我阅读了这个,但它实际上不包括我想要的方法。此外,否定实际值不适用于我的用例。
Heapq 文档:https://docs.python.org/3.6/library/heapq.html
假设我的堆中有一个数据类对象列表。只有 a
属性 决定对象的顺序。
import heapq
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a < other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
print(heapq.heappop(l)) # C(a=2, b=1)
print(heapq.heappop(l)) # C(a=2, b=4)
print(heapq.heappop(l)) # C(a=9, b=109)
print(heapq.heappop(l)) # C(a=9, b=4)
现在我想要倒序。因此,我将行 return self.a < other.a
更改为 return self.a > other.a
。结果:
import heapq
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a > other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
print(heapq.heappop(l)) # C(a=2, b=1)
print(heapq.heappop(l)) # C(a=9, b=109)
print(heapq.heappop(l)) # C(a=9, b=4)
print(heapq.heappop(l)) # C(a=2, b=4)
期望的结果应该是四种解决方案之一:
C(a=9, b=109) C(a=9, b=4) C(a=9, b=109) C(a=9, b=4)
C(a=9, b=4) C(a=9, b=109) C(a=9, b=4) C(a=9, b=109)
C(a=2, b=1) C(a=2, b=1) C(a=2, b=4) C(a=2, b=4)
C(a=2, b=4) C(a=2, b=4) C(a=2, b=1) C(a=2, b=1)
可能并非所有对象对都通过 heapq
进行比较,这可以解释奇怪的顺序。但是,还有可能得到倒序吗?
是否必须提供更多的对象比较方法?
object.__lt__(self, other)
object.__le__(self, other)
object.__eq__(self, other)
object.__ne__(self, other)
object.__gt__(self, other)
object.__ge__(self, other)
如果您有完全不同的方法,请不要犹豫!
您需要使用 heapify
将 l
变成一个堆
from heapq import heapify, heappop
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a > other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
heapify(l)
while l:
print(heappop(l))
打印
C(a=9, b=4)
C(a=9, b=109)
C(a=2, b=1)
C(a=2, b=4)
首先,我阅读了这个
Heapq 文档:https://docs.python.org/3.6/library/heapq.html
假设我的堆中有一个数据类对象列表。只有 a
属性 决定对象的顺序。
import heapq
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a < other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
print(heapq.heappop(l)) # C(a=2, b=1)
print(heapq.heappop(l)) # C(a=2, b=4)
print(heapq.heappop(l)) # C(a=9, b=109)
print(heapq.heappop(l)) # C(a=9, b=4)
现在我想要倒序。因此,我将行 return self.a < other.a
更改为 return self.a > other.a
。结果:
import heapq
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a > other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
print(heapq.heappop(l)) # C(a=2, b=1)
print(heapq.heappop(l)) # C(a=9, b=109)
print(heapq.heappop(l)) # C(a=9, b=4)
print(heapq.heappop(l)) # C(a=2, b=4)
期望的结果应该是四种解决方案之一:
C(a=9, b=109) C(a=9, b=4) C(a=9, b=109) C(a=9, b=4)
C(a=9, b=4) C(a=9, b=109) C(a=9, b=4) C(a=9, b=109)
C(a=2, b=1) C(a=2, b=1) C(a=2, b=4) C(a=2, b=4)
C(a=2, b=4) C(a=2, b=4) C(a=2, b=1) C(a=2, b=1)
可能并非所有对象对都通过 heapq
进行比较,这可以解释奇怪的顺序。但是,还有可能得到倒序吗?
是否必须提供更多的对象比较方法?
object.__lt__(self, other)
object.__le__(self, other)
object.__eq__(self, other)
object.__ne__(self, other)
object.__gt__(self, other)
object.__ge__(self, other)
如果您有完全不同的方法,请不要犹豫!
您需要使用 heapify
l
变成一个堆
from heapq import heapify, heappop
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a > other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
heapify(l)
while l:
print(heappop(l))
打印
C(a=9, b=4)
C(a=9, b=109)
C(a=2, b=1)
C(a=2, b=4)