【发布时间】:2018-11-23 16:23:27
【问题描述】:
首先,我阅读了这个SO question,但它实际上并没有包含我想要的方法。此外,否定实际值不适用于我的用例。
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)
如果您有完全不同的方法,请不要犹豫!
【问题讨论】:
标签: python python-3.x heap python-3.6