[issue5669] Extra heap nlargest/nsmallest option for including ties

George Sakkis report at bugs.python.org
Thu Apr 2 18:43:18 CEST 2009


New submission from George Sakkis <george.sakkis at gmail.com>:

It would be useful in many cases if heapq.nlargest and heapq.nsmallest
grew an optional boolean parameter, say `ties` (defaulting to False)
that when True, it would return more than `n` items if there are ties.
To illustrate:

>>> s = [4,3,5,7,4,7,4,3]
>>> for i in xrange(1,len(s)+1): print i,heapq.nsmallest(i,s)
...
1 [3]
2 [3, 3]
3 [3, 3, 4]
4 [3, 3, 4, 4]
5 [3, 3, 4, 4, 4]
6 [3, 3, 4, 4, 4, 5]
7 [3, 3, 4, 4, 4, 5, 7]
8 [3, 3, 4, 4, 4, 5, 7, 7]

>>> for i in xrange(1,len(s)+1): print i,heapq.nsmallest(i,s)
...
1 [3, 3]
2 [3, 3]
3 [3, 3, 4, 4, 4]
4 [3, 3, 4, 4, 4]
5 [3, 3, 4, 4, 4]
6 [3, 3, 4, 4, 4, 5]
7 [3, 3, 4, 4, 4, 5, 7, 7]
8 [3, 3, 4, 4, 4, 5, 7, 7]

----------
components: Library (Lib)
messages: 85222
nosy: gsakkis
severity: normal
status: open
title: Extra heap nlargest/nsmallest option for including ties
type: feature request
versions: Python 2.7, Python 3.1

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5669>
_______________________________________


More information about the Python-bugs-list mailing list