diff options
Diffstat (limited to 'Lib/heapq.py')
-rw-r--r-- | Lib/heapq.py | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/Lib/heapq.py b/Lib/heapq.py index f3d0669..48697f6 100644 --- a/Lib/heapq.py +++ b/Lib/heapq.py @@ -351,7 +351,8 @@ def nsmallest(n, iterable, key=None): Equivalent to: sorted(iterable, key=key)[:n] """ in1, in2 = tee(iterable) - it = izip(map(key, in1), count(), in2) # decorate + keys = in1 if key is None else map(key, in1) + it = izip(keys, count(), in2) # decorate result = _nsmallest(n, it) return list(map(itemgetter(2), result)) # undecorate @@ -362,7 +363,8 @@ def nlargest(n, iterable, key=None): Equivalent to: sorted(iterable, key=key, reverse=True)[:n] """ in1, in2 = tee(iterable) - it = izip(map(key, in1), map(neg, count()), in2) # decorate + keys = in1 if key is None else map(key, in1) + it = izip(keys, map(neg, count()), in2) # decorate result = _nlargest(n, it) return list(map(itemgetter(2), result)) # undecorate |