Bugs item #1158313, was opened at 2005-03-07 09:15 Message generated for change (Comment added) made by rhettinger You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1158313&group_id=5470
Category: Python Library Group: Feature Request Status: Open Resolution: None Priority: 5 Submitted By: Stefan Behnel (scoder) Assigned to: Nobody/Anonymous (nobody) Summary: heapq should provide iterators: itersmallest, iterlargest Initial Comment: The current C-based heapq implementation provides implementations of min-heaps and max-heaps for the "nsmallest" and "nlargest" functions. I would like to see them used to provide iterators over the smallest/largest items of a heap: "itersmallest(heap)" and "iterlargest(heap)". Why: The functions nsmallest() and nlargest() are efficient (and sufficient) if n is known. However, if n is *not* known in advance, it would still be more efficient than sorting to run heapify() over a list and then have an iterator run heappop on each iteration. While this is easily implemented for min-heaps using heappop, max-heaps are not part of the Python-Implementation. Currently, they are only implemented in C. Possible counter-arguments: The straight-forward iterator implementation will manipulate the heap. This could be seen as a side-effect. It should, however, be enough to document that. Similar problems are documented for mutating sequences during iteration. ---------------------------------------------------------------------- >Comment By: Raymond Hettinger (rhettinger) Date: 2005-03-07 11:22 Message: Logged In: YES user_id=80475 The idea for introducing heapiter() function was unsuccessfully proposed on python-dev: http://mail.python.org/pipermail/python-dev/2004-June/045348.html The use cases were rare and the pure python solution was both easy and fast. If you need C coded max-heaps, that could be a separate feature request. There would need to be sufficient use cases to warrant building out the module's API. ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1158313&group_id=5470 _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com