diff options
author | Cheryl Sabella <cheryl.sabella@gmail.com> | 2018-03-27 01:29:33 (GMT) |
---|---|---|
committer | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2018-03-27 01:29:33 (GMT) |
commit | da1734c58d2f97387ccc9676074717d38b044128 (patch) | |
tree | c74f3088c65e19e87e193cbd391036f591f2dec8 /Doc | |
parent | 834940375ae88bc95794226dd8eff1f25fba1cf9 (diff) | |
download | cpython-da1734c58d2f97387ccc9676074717d38b044128.zip cpython-da1734c58d2f97387ccc9676074717d38b044128.tar.gz cpython-da1734c58d2f97387ccc9676074717d38b044128.tar.bz2 |
bpo-27212: Modify islice recipe to consume initial values preceding start (GH-6195)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/itertools.rst | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 0b3829f..a5a5356 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -436,15 +436,24 @@ loops that truncate the stream. # islice('ABCDEFG', 2, None) --> C D E F G # islice('ABCDEFG', 0, None, 2) --> A C E G s = slice(*args) - it = iter(range(s.start or 0, s.stop or sys.maxsize, s.step or 1)) + start, stop, step = s.start or 0, s.stop or sys.maxsize, s.step or 1 + it = iter(range(start, stop, step)) try: nexti = next(it) except StopIteration: + # Consume *iterable* up to the *start* position. + for i, element in zip(range(start), iterable): + pass return - for i, element in enumerate(iterable): - if i == nexti: - yield element - nexti = next(it) + try: + for i, element in enumerate(iterable): + if i == nexti: + yield element + nexti = next(it) + except StopIteration: + # Consume to *stop*. + for i, element in zip(range(i + 1, stop), iterable): + pass If *start* is ``None``, then iteration starts at zero. If *step* is ``None``, then the step defaults to one. @@ -688,8 +697,8 @@ which incur interpreter overhead. # tail(3, 'ABCDEFG') --> E F G return iter(collections.deque(iterable, maxlen=n)) - def consume(iterator, n): - "Advance the iterator n-steps ahead. If n is none, consume entirely." + def consume(iterator, n=None): + "Advance the iterator n-steps ahead. If n is None, consume entirely." # Use functions that consume iterators at C speed. if n is None: # feed the entire iterator into a zero-length deque |