summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-01-22 23:25:35 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-01-22 23:25:35 (GMT)
commit1dfde1ddc0e1980d67bd19e187252d4e52b4f7ce (patch)
tree365fcd0e0d95e4ff612ca7184d441e6184f8b203 /Lib
parent86def6cb2b8d6d8c7f239795fa7af57c97a5890d (diff)
downloadcpython-1dfde1ddc0e1980d67bd19e187252d4e52b4f7ce.zip
cpython-1dfde1ddc0e1980d67bd19e187252d4e52b4f7ce.tar.gz
cpython-1dfde1ddc0e1980d67bd19e187252d4e52b4f7ce.tar.bz2
Replace map(None, *iterables) with zip(*iterables).
Diffstat (limited to 'Lib')
-rw-r--r--Lib/heapq.py6
-rw-r--r--Lib/test/test_builtin.py21
-rw-r--r--Lib/test/test_iter.py8
-rw-r--r--Lib/test/test_itertools.py11
4 files changed, 11 insertions, 35 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
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 2718bbf..e6ded81 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -1100,18 +1100,6 @@ class BuiltinTest(unittest.TestCase):
def test_map(self):
self.assertEqual(
- list(map(None, 'hello')),
- [('h',), ('e',), ('l',), ('l',), ('o',)]
- )
- self.assertEqual(
- list(map(None, 'abcd', 'efg')),
- [('a', 'e'), ('b', 'f'), ('c', 'g')]
- )
- self.assertEqual(
- list(map(None, range(3))),
- [(0,), (1,), (2,)]
- )
- self.assertEqual(
list(map(lambda x: x*x, range(1,4))),
[1, 4, 9]
)
@@ -1146,17 +1134,9 @@ class BuiltinTest(unittest.TestCase):
[1+4+1, 3+9+1, 7+2+0]
)
self.assertEqual(
- list(map(None, Squares(10))),
- [(0,), (1,), (4,), (9,), (16,), (25,), (36,), (49,), (64,), (81,)]
- )
- self.assertEqual(
list(map(int, Squares(10))),
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
)
- self.assertEqual(
- list(map(None, Squares(3), Squares(2))),
- [(0,0), (1,1)]
- )
def Max(a, b):
if a is None:
return b
@@ -1169,7 +1149,6 @@ class BuiltinTest(unittest.TestCase):
)
self.assertRaises(TypeError, map)
self.assertRaises(TypeError, map, lambda x: x, 42)
- self.assertEqual(list(map(None, [42])), [(42,)])
class BadSeq:
def __iter__(self):
raise ValueError
diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py
index 07a3bf2..d861dcd 100644
--- a/Lib/test/test_iter.py
+++ b/Lib/test/test_iter.py
@@ -382,13 +382,10 @@ class TestCase(unittest.TestCase):
# Test map()'s use of iterators.
def test_builtin_map(self):
- self.assertEqual(list(map(None, SequenceClass(5))),
- [(0,), (1,), (2,), (3,), (4,)])
self.assertEqual(list(map(lambda x: x+1, SequenceClass(5))),
list(range(1, 6)))
d = {"one": 1, "two": 2, "three": 3}
- self.assertEqual(list(map(None, d)), [(k,) for k in d])
self.assertEqual(list(map(lambda k, d=d: (k, d[k]), d)),
list(d.items()))
dkeys = list(d.keys())
@@ -396,11 +393,6 @@ class TestCase(unittest.TestCase):
i,
i < len(d) and dkeys[i] or None)
for i in range(3)]
- self.assertEqual(list(map(None,
- d,
- SequenceClass(5),
- iter(d.keys()))),
- expected)
f = open(TESTFN, "w")
try:
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index 4c0af07..ae5eb90 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -236,7 +236,7 @@ class TestBasicOps(unittest.TestCase):
self.assertEqual(list(izip_longest('abcdef')), list(zip('abcdef')))
self.assertEqual(list(izip_longest('abc', 'defg', **{})),
- list(map(None, list('abc')+[None], 'defg'))) # empty keyword dict
+ list(izip(list('abc')+[None], 'defg'))) # empty keyword dict
self.assertRaises(TypeError, izip_longest, 3)
self.assertRaises(TypeError, izip_longest, range(3), 3)
@@ -281,14 +281,17 @@ class TestBasicOps(unittest.TestCase):
def test_imap(self):
self.assertEqual(list(imap(operator.pow, range(3), range(1,7))),
[0**1, 1**2, 2**3])
- self.assertEqual(list(imap(None, 'abc', range(5))),
+ def tupleize(*args):
+ return args
+ self.assertEqual(list(imap(tupleize, 'abc', range(5))),
[('a',0),('b',1),('c',2)])
- self.assertEqual(list(imap(None, 'abc', count())),
+ self.assertEqual(list(imap(tupleize, 'abc', count())),
[('a',0),('b',1),('c',2)])
- self.assertEqual(take(2,imap(None, 'abc', count())),
+ self.assertEqual(take(2,imap(tupleize, 'abc', count())),
[('a',0),('b',1)])
self.assertEqual(list(imap(operator.pow, [])), [])
self.assertRaises(TypeError, imap)
+ self.assertRaises(TypeError, list, imap(None, range(3), range(3)))
self.assertRaises(TypeError, imap, operator.neg)
self.assertRaises(TypeError, next, imap(10, range(5)))
self.assertRaises(ValueError, next, imap(errfunc, [4], [5]))