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

2009-04-05 Thread George Sakkis
George Sakkis added the comment: Posted recipe at http://code.activestate.com/recipes/576712/. You were right, the implementation gets significantly hairier but I think it's worth having this option. It's also faster than using sorted/bisect as len(seq)/N increases and there is no pathologically

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

2009-04-02 Thread Raymond Hettinger
Raymond Hettinger added the comment: Here's a test case for your recipe dataset = [9] * 100 + [0,1,2,3,4,5,6,8] for i in range(1, 20): print nsmallest(i, dataset, ties=True) -- ___ Python tracker _

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

2009-04-02 Thread George Sakkis
George Sakkis added the comment: > I recommend posting an ASPN recipe. That's what I do with a lot of > ideas that are under development or that don't clear the bar for going > into the standard library. Will do. Thanks for the quick turnaround! -- ___

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

2009-04-02 Thread Raymond Hettinger
Raymond Hettinger added the comment: FWIW, an easy way to pare down millions of entries to dozens is to use a bigger heap: from heapq import nsmallest def nsmallest_with_ties(n, iterable, scale=2): ext = n * scale s = nsmallest(ext, iterable) lastplace = s[n-1] if s[-1] == last

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

2009-04-02 Thread Raymond Hettinger
Raymond Hettinger added the comment: I recommend posting an ASPN recipe. That's what I do with a lot of ideas that are under development or that don't clear the bar for going into the standard library. -- ___ Python tracker

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

2009-04-02 Thread George Sakkis
Changes by George Sakkis : -- status: open -> closed ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail

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

2009-04-02 Thread George Sakkis
George Sakkis added the comment: > That should have been: last = result[-1]; [last]*s.count(last). Nice, though that's not equivalent if the objects' identity is significant for what happens next (which typically is the case when ties are preserved). The sorted/bisect solution doesn't have thi

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

2009-04-02 Thread George Sakkis
George Sakkis added the comment: > In that case, I think it best to leave nsmallest/nlargest as-is. By > appending ties to the result, it becomes harder to implement policy > decisions on what to do with those ties (perhaps listing them separately > or splitting their prizes n-ways). I woul

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

2009-04-02 Thread Raymond Hettinger
Raymond Hettinger added the comment: That should have been: last = result[-1]; [last]*s.count(last). -- ___ Python tracker ___ ___ Py

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

2009-04-02 Thread Raymond Hettinger
Raymond Hettinger added the comment: In that case, I think it best to leave nsmallest/nlargest as-is. By appending ties to the result, it becomes harder to implement policy decisions on what to do with those ties (perhaps listing them separately or splitting their prizes n-ways). A preferred

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

2009-04-02 Thread George Sakkis
George Sakkis added the comment: There's nothing special about my use cases; I'd even go as far as to suggest that this is more often than not the desired behavior in general. Say that you have to select the top 3 chess players and there are two players with equal Elo rating at positions 3 and

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

2009-04-02 Thread Raymond Hettinger
Raymond Hettinger added the comment: I haven't seen this come up before. Am curious about your use cases. -- ___ Python tracker ___ _

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

2009-04-02 Thread Raymond Hettinger
Changes by Raymond Hettinger : -- assignee: -> rhettinger nosy: +rhettinger ___ Python tracker ___ ___ Python-bugs-list mailing list U

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

2009-04-02 Thread George Sakkis
Changes by George Sakkis : -- title: Extra heap nlargest/nsmallest option for including ties -> Extra heapq nlargest/nsmallest option for including ties ___ Python tracker ___ __