summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2009-02-21 23:20:57 (GMT)
committerRaymond Hettinger <python@rcn.com>2009-02-21 23:20:57 (GMT)
commitba86fa9d5f24aa33f994d3b7a4bd31fdd8cf70e8 (patch)
tree08e664e1e07258ced67c1968358c98704f844f8d
parent912fbcade3789b6a3dff75f46e564bbe74d0c20b (diff)
downloadcpython-ba86fa9d5f24aa33f994d3b7a4bd31fdd8cf70e8.zip
cpython-ba86fa9d5f24aa33f994d3b7a4bd31fdd8cf70e8.tar.gz
cpython-ba86fa9d5f24aa33f994d3b7a4bd31fdd8cf70e8.tar.bz2
In Py3.x, a list comprehension is now faster than list(map(itemgetter(0), iterable)).
-rw-r--r--Lib/heapq.py9
1 files changed, 4 insertions, 5 deletions
diff --git a/Lib/heapq.py b/Lib/heapq.py
index b361631..19a457b 100644
--- a/Lib/heapq.py
+++ b/Lib/heapq.py
@@ -130,7 +130,6 @@ __all__ = ['heappush', 'heappop', 'heapify', 'heapreplace', 'merge',
'nlargest', 'nsmallest', 'heappushpop']
from itertools import islice, repeat, count, tee, chain
-from operator import itemgetter
import bisect
def heappush(heap, item):
@@ -377,13 +376,13 @@ def nsmallest(n, iterable, key=None):
if key is None:
it = zip(iterable, count()) # decorate
result = _nsmallest(n, it)
- return list(map(itemgetter(0), result)) # undecorate
+ return [r[0] for r in result] # undecorate
# General case, slowest method
in1, in2 = tee(iterable)
it = zip(map(key, in1), count(), in2) # decorate
result = _nsmallest(n, it)
- return list(map(itemgetter(2), result)) # undecorate
+ return [r[2] for r in result] # undecorate
_nlargest = nlargest
def nlargest(n, iterable, key=None):
@@ -415,13 +414,13 @@ def nlargest(n, iterable, key=None):
if key is None:
it = zip(iterable, count(0,-1)) # decorate
result = _nlargest(n, it)
- return list(map(itemgetter(0), result)) # undecorate
+ return [r[0] for r in result] # undecorate
# General case, slowest method
in1, in2 = tee(iterable)
it = zip(map(key, in1), count(0,-1), in2) # decorate
result = _nlargest(n, it)
- return list(map(itemgetter(2), result)) # undecorate
+ return [r[2] for r in result] # undecorate
if __name__ == "__main__":
# Simple sanity test