New submission from George Sakkis <george.sak...@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 <rep...@bugs.python.org> <http://bugs.python.org/issue5669> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com