mirror of
https://github.com/python/cpython.git
synced 2025-11-25 04:34:37 +00:00
Manually merge r68096,68189 from 3.0 branch.
This commit is contained in:
parent
3f5f8228c0
commit
3a9b062f5b
4 changed files with 19 additions and 5 deletions
14
Lib/heapq.py
14
Lib/heapq.py
|
|
@ -354,9 +354,12 @@ def nsmallest(n, iterable, key=None):
|
|||
|
||||
Equivalent to: sorted(iterable, key=key)[:n]
|
||||
"""
|
||||
if key is None:
|
||||
it = zip(iterable, count()) # decorate
|
||||
result = _nsmallest(n, it)
|
||||
return list(map(itemgetter(0), result)) # undecorate
|
||||
in1, in2 = tee(iterable)
|
||||
keys = in1 if key is None else map(key, in1)
|
||||
it = zip(keys, count(), in2) # decorate
|
||||
it = zip(map(key, in1), count(), in2) # decorate
|
||||
result = _nsmallest(n, it)
|
||||
return list(map(itemgetter(2), result)) # undecorate
|
||||
|
||||
|
|
@ -366,9 +369,12 @@ def nlargest(n, iterable, key=None):
|
|||
|
||||
Equivalent to: sorted(iterable, key=key, reverse=True)[:n]
|
||||
"""
|
||||
if key is None:
|
||||
it = zip(iterable, map(neg, count())) # decorate
|
||||
result = _nlargest(n, it)
|
||||
return list(map(itemgetter(0), result)) # undecorate
|
||||
in1, in2 = tee(iterable)
|
||||
keys = in1 if key is None else map(key, in1)
|
||||
it = zip(keys, map(neg, count()), in2) # decorate
|
||||
it = zip(map(key, in1), map(neg, count()), in2) # decorate
|
||||
result = _nlargest(n, it)
|
||||
return list(map(itemgetter(2), result)) # undecorate
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue