Python heapq 模块,对象上的 heapify 方法
因为我试图在我正在制作的程序中提高效率,所以我想我应该使用 python 中内置的 heapq 模块,但是我的一些对象具有多个属性,例如名称和编号。有没有办法使用 heapify 方法根据某个属性来堆化我的对象?我在文档中没有看到任何内容。
Since I'm trying to be efficient in this program I'm making, I thought I'd use the built in heapq module in python, but some of my objects have multiple attributes, like name and number. Is there a way to use the heapify method to heapify my objects based on a certain attribute? I don't see anything in the documentation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我发布之后,我想你可以在使用 heapify 之前根据所需的属性创建一个对象列表,这将花费 O(n) 线性时间。这不会影响 heapify 或其他 heapq 方法的运行时。
Right after I posted, I figured you could make a list of the objects by the attribute needed before using heapify which would take O(n) linear time. This wouldn't affect the runtime of heapify or other heapq methods.
@vsekhar 和@所有其他人都想知道已接受的答案。
假设:
现在,不是创建一个仅将对象作为元素的堆:
您实际上想要创建一个包含 2 个值的元组作为堆元素的堆 - 这个想法是将要排序的属性作为该属性的第一个值元组和对象(如前所述)作为元组的第二个元素:
堆将元组的第一个值视为排序依据的值。
heap
的元素不是像int
或str
这样的简单数据类型,底层实现需要知道如何比较元素。元组
)另一个选项可能是使比较与您的自定义类一起工作 - 可以实现此操作,以便对象本身可以用作堆元素(如在第一个例子中)。
SomeObject
:这样您就可以创建仅将对象作为元素的堆:
@vsekhar and @ all the others wondering about the accepted answer.
Assumption:
Now, instead of creating a heap with the objects only as elements:
you actually want to create the heap with a tuple of 2 values as heap elements - The idea is to have the attribute you want to sort with as first value of the tuple and the object (as before) as the second element of the tuple:
The
heap
considers this first value of the tuple as the value to sort by.heap
is not of a simple data type likeint
orstr
, the underlying implementation needs to know how to compare elements.tuple
)Another option might be to make comparison work with your custom class - This can be implemented so the object itself can be used as the heap element (as in the first example).
SomeObject
:This way you can create the heap with the objects only as elements: