关于heapq模块的注释 [英] A note on heapq module
问题描述
在几分钟内我刚写了这个非常原始的类,它缺少
doctring(与heapq模块的功能相同),它可能
包含bug不过,我还没有测试过多少。它只是一个简单的
包装器,它包含了一些heapq模块的功能(nsmallest和
nlargest缺失)。通常我只在我需要的时候才使用类,我就像函数式编程一样充足了这个类,而且这个类看起来只是减慢了heapq模块的功能。但是我认为改进的类似于这个类似乎很有用(添加,不需要替换)
到heapq模块中,因为它可以避免cetain错误:我知道什么是
堆是什么以及它们是如何工作的,但前段时间我已经将一个错误
写入了使用heapq模块功能的小程序,因为
我使用普通列表
方法将一个项目添加到堆头,打破堆不变量。有了这样一个简单的类
你对象的所有方法都保持堆不变,避免了这种错误。 (如果你有兴趣我可以改进这个课程,
并不困难。)
来自heapq import heapify,heappush,heappop,heapreplace
class Heap(object):
def __init __(self,init = None):
如果init为None:
self.h = []
elif isinstance(init,Heap):
self.h = init.h [:]
:否则:
self.h = list(init)
heapify(self.h)
def最小(自我):
返回self.h [0]
def sort(self,cmp = None,key = None):
self.h.sort(cmp = cmp,key = key)
def heappush(self,item):
heappush(self.h,item)
def heappop(self ):
返回heappop(self.h)
def heapreplace(self,item):
返回heapreplace(self.h,item)
def clear(self):
del self.h [:]
def __contains __(self,item):
返回self.h中的项目
def __hash __(self):
引发TypeError(" Heap ob jects是不可用的。)
def __iter __(自我):
返回self.h .__ iter __()
def __eq __(自我,其他):
返回isinstance(其他,堆)和self.h == other.h
def __ne __(自我,其他):
不返回isinstance(其他,Heap)或self.h!= other.h
def __len __(self):
返回len(self.h)
def __nonzero __(自我):
返回bool(self.h)
def __str __(自我):
返回str (self.h)
def __repr __(self):
return" Heap(%s)" %self.h如果self.h elseHeap()
再见,
熊宝宝
In few minutes I have just written this quite raw class, it lacks
doctring (the same of the functions of the heapq module), it may
contain bugs still, I haven''t tested it much. It''s just a simple
wrapper around some of the functions of the heapq module (nsmallest and
nlargest are missing). Usually I use classes only when I need then, I
like functional programming enough, and this class seems to just slow
down the functions of the heapq module. But I think an improved class
similar to this one may be useful (added, replacing isn''t necessary)
into the heapq module, because it can avoid cetain errors: I know what
Heaps are and how they work, but some time ago I have written a bug
into small program that uses the functions of the heapq module, because
I have added an item to the head of the heap using a normal list
method, breaking the heap invariant. With a simple class like this one
all the methods of your object keep the heap invariant, avoiding that
kind of bugs. (If you are interested I can improve this class some,
it''t not difficult.)
from heapq import heapify, heappush, heappop, heapreplace
class Heap(object):
def __init__(self, init=None):
if init is None:
self.h = []
elif isinstance(init, Heap):
self.h = init.h[:]
else:
self.h = list(init)
heapify(self.h)
def smallest(self):
return self.h[0]
def sort(self, cmp=None, key=None):
self.h.sort(cmp=cmp, key=key)
def heappush(self, item):
heappush(self.h, item)
def heappop(self):
return heappop(self.h)
def heapreplace(self, item):
return heapreplace(self.h, item)
def clear(self):
del self.h[:]
def __contains__(self, item):
return item in self.h
def __hash__(self):
raise TypeError("Heap objects are unhashable.")
def __iter__(self):
return self.h.__iter__()
def __eq__(self, other):
return isinstance(other, Heap) and self.h == other.h
def __ne__(self, other):
return not isinstance(other, Heap) or self.h != other.h
def __len__(self):
return len(self.h)
def __nonzero__(self):
return bool(self.h)
def __str__(self):
return str(self.h)
def __repr__(self):
return "Heap(%s)" % self.h if self.h else "Heap()"
Bye,
bearophile
推荐答案
是********* ***@lycos.com 写道:
前段时间我写了一个错误
到小程序中使用heapq模块的功能,因为
我使用普通列表
方法将一个项添加到堆头,打破堆不变量。
some time ago I have written a bug
into small program that uses the functions of the heapq module, because
I have added an item to the head of the heap using a normal list
method, breaking the heap invariant.
我知道我之前的代码中有类似的错误。
I know I''ve had similar bugs in my code before.
来自heapq import heapify的
,heappush,heappop,heapreplace
class Heap(object):
from heapq import heapify, heappush, heappop, heapreplace
class Heap(object):
[snip implementation]
+1用于将类似的Heap对象添加到collections模块。如果你可以花一点时间来添加一些测试(你可能会从test_heapq.py中偷走很多
你需要的东西)你应该把它作为
a 2.6补丁。
STeVe
[snip implementation]
+1 for adding a Heap object like this to the collections module. If you
can make a little time to add some tests (you could probably steal a lot
of what you need from test_heapq.py) you should definitely submit it as
a patch for 2.6.
STeVe
我认为必须简化排序,否则它不能保持堆积
不变...
def sort(self):
self.h.sort ()
再见,
熊宝宝
I think the sort has to be simplified, otherwise it can''t keep the heap
invariant...
def sort(self):
self.h.sort()
Bye,
bearophile
是************ @ lycos.com kirjoitti:
be************@lycos.com kirjoitti:
我认为必须简化排序,否则它不能保持堆积
不变...
def sort(self):
self.h.sort()
再见,
熊宝宝
I think the sort has to be simplified, otherwise it can''t keep the heap
invariant...
def sort(self):
self.h.sort()
Bye,
bearophile
和__repr__应该是这样的:
=========
def __repr __(self):
如果self.h:
return"堆(%s)" %self.h
else:
return" Heap()"
==========
使其与2.5之前的版本兼容
干杯,
Jussi
And __repr__ should be something like this:
=========
def __repr__(self):
if self.h:
return "Heap(%s)" % self.h
else:
return "Heap()"
==========
to make it compatible with versions earlier than 2.5
Cheers,
Jussi
这篇关于关于heapq模块的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!