Update itertools recipes to use next().

This commit is contained in:
Raymond Hettinger 2009-02-23 19:32:55 +00:00
parent 52bc7b85fd
commit d47442e3cb
1 changed files with 6 additions and 7 deletions

View File

@ -321,14 +321,14 @@ loops that truncate the stream.
return self return self
def next(self): def next(self):
while self.currkey == self.tgtkey: while self.currkey == self.tgtkey:
self.currvalue = self.it.next() # Exit on StopIteration self.currvalue = next(self.it) # Exit on StopIteration
self.currkey = self.keyfunc(self.currvalue) self.currkey = self.keyfunc(self.currvalue)
self.tgtkey = self.currkey self.tgtkey = self.currkey
return (self.currkey, self._grouper(self.tgtkey)) return (self.currkey, self._grouper(self.tgtkey))
def _grouper(self, tgtkey): def _grouper(self, tgtkey):
while self.currkey == tgtkey: while self.currkey == tgtkey:
yield self.currvalue yield self.currvalue
self.currvalue = self.it.next() # Exit on StopIteration self.currvalue = next(self.it) # Exit on StopIteration
self.currkey = self.keyfunc(self.currvalue) self.currkey = self.keyfunc(self.currvalue)
.. versionadded:: 2.4 .. versionadded:: 2.4
@ -378,7 +378,7 @@ loops that truncate the stream.
# imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000 # imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000
iterables = map(iter, iterables) iterables = map(iter, iterables)
while True: while True:
args = [it.next() for it in iterables] args = [next(it) for it in iterables]
if function is None: if function is None:
yield tuple(args) yield tuple(args)
else: else:
@ -404,11 +404,11 @@ loops that truncate the stream.
# islice('ABCDEFG', 0, None, 2) --> A C E G # islice('ABCDEFG', 0, None, 2) --> A C E G
s = slice(*args) s = slice(*args)
it = iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1)) it = iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1))
nexti = it.next() nexti = next(it)
for i, element in enumerate(iterable): for i, element in enumerate(iterable):
if i == nexti: if i == nexti:
yield element yield element
nexti = it.next() nexti = next(it)
If *start* is ``None``, then iteration starts at zero. If *step* is ``None``, If *start* is ``None``, then iteration starts at zero. If *step* is ``None``,
then the step defaults to one. then the step defaults to one.
@ -738,8 +738,7 @@ which incur interpreter overhead.
def pairwise(iterable): def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..." "s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable) a, b = tee(iterable)
for elem in b: next(b, None)
break
return izip(a, b) return izip(a, b)
def grouper(n, iterable, fillvalue=None): def grouper(n, iterable, fillvalue=None):