Skip to content

Commit

Permalink
Port r69838: Speedup and simplify negative counter using count's new …
Browse files Browse the repository at this point in the history
…step argument.
  • Loading branch information
rhettinger committed Feb 21, 2009
1 parent 934896d commit bd171bc
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Lib/heapq.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
'nlargest', 'nsmallest', 'heappushpop']

from itertools import islice, repeat, count, tee, chain
from operator import itemgetter, neg
from operator import itemgetter
import bisect

def heappush(heap, item):
Expand Down Expand Up @@ -413,13 +413,13 @@ def nlargest(n, iterable, key=None):

# When key is none, use simpler decoration
if key is None:
it = zip(iterable, map(neg, count())) # decorate
it = zip(iterable, count(0,-1)) # decorate
result = _nlargest(n, it)
return list(map(itemgetter(0), result)) # undecorate

# General case, slowest method
in1, in2 = tee(iterable)
it = zip(map(key, in1), map(neg, count()), in2) # decorate
it = zip(map(key, in1), count(0,-1), in2) # decorate
result = _nlargest(n, it)
return list(map(itemgetter(2), result)) # undecorate

Expand Down

0 comments on commit bd171bc

Please sign in to comment.