mirror of
https://github.com/python/cpython.git
synced 2025-08-01 07:33:08 +00:00
Speedup and simplify negative counter using count's new step argument.
This commit is contained in:
parent
aa681c7b99
commit
be9b765c07
1 changed files with 3 additions and 3 deletions
|
@ -130,7 +130,7 @@ __all__ = ['heappush', 'heappop', 'heapify', 'heapreplace', 'merge',
|
||||||
'nlargest', 'nsmallest', 'heappushpop']
|
'nlargest', 'nsmallest', 'heappushpop']
|
||||||
|
|
||||||
from itertools import islice, repeat, count, imap, izip, tee, chain
|
from itertools import islice, repeat, count, imap, izip, tee, chain
|
||||||
from operator import itemgetter, neg
|
from operator import itemgetter
|
||||||
import bisect
|
import bisect
|
||||||
|
|
||||||
def heappush(heap, item):
|
def heappush(heap, item):
|
||||||
|
@ -413,13 +413,13 @@ def nlargest(n, iterable, key=None):
|
||||||
|
|
||||||
# When key is none, use simpler decoration
|
# When key is none, use simpler decoration
|
||||||
if key is None:
|
if key is None:
|
||||||
it = izip(iterable, imap(neg, count())) # decorate
|
it = izip(iterable, count(0,-1)) # decorate
|
||||||
result = _nlargest(n, it)
|
result = _nlargest(n, it)
|
||||||
return map(itemgetter(0), result) # undecorate
|
return map(itemgetter(0), result) # undecorate
|
||||||
|
|
||||||
# General case, slowest method
|
# General case, slowest method
|
||||||
in1, in2 = tee(iterable)
|
in1, in2 = tee(iterable)
|
||||||
it = izip(imap(key, in1), imap(neg, count()), in2) # decorate
|
it = izip(imap(key, in1), count(0,-1), in2) # decorate
|
||||||
result = _nlargest(n, it)
|
result = _nlargest(n, it)
|
||||||
return map(itemgetter(2), result) # undecorate
|
return map(itemgetter(2), result) # undecorate
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue