Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82912

Re: learning to use iterators

Path csiph.com!usenet.pasdenom.info!news.franciliens.net!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'correct.': 0.07; 'none)': 0.09; 'preferable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sequences.': 0.09; 'def': 0.12; 'anyway': 0.14; '24,': 0.16; '3):': 0.16; 'garbage': 0.16; 'incorrect': 0.16; 'iterable': 0.16; 'iterables': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'itertools': 0.16; 'notation': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'succinct': 0.16; 'wrote:': 0.18; 'wed,': 0.18; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'raise': 0.29; 'dec': 0.30; 'start,': 0.30; 'subject:learning': 0.31; 'problem': 0.35; "can't": 0.35; 'but': 0.35; 'version': 0.36; 'really': 0.36; 'opposed': 0.36; 'returning': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'ian': 0.60; 'results.': 0.60; 'course': 0.61; 'skip:n 10': 0.64; 'more': 0.64; 'natural': 0.68; 'hand': 0.80; 'abc': 0.84; 'n):': 0.84; 'silently': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: learning to use iterators
Date Thu, 25 Dec 2014 11:34:38 +0100
Organization None
References <878uhyb3hq.fsf@net82.ceos.umanitoba.ca> <mailman.17185.1419433939.18130.python-list@python.org> <8a840698-a3be-4889-9ddd-756a553e6f48@googlegroups.com> <CALwzid=VT78RHJVuznZ+nWKBuYMZsh0z7dt9Ay2pKUbjN0zaXw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bdbf45.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17198.1419503693.18130.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1419503693 news.xs4all.nl 2875 [2001:888:2000:d::a6]:45732
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:82912

Show key headers only | View raw


Ian Kelly wrote:

> On Wed, Dec 24, 2014 at 1:34 PM, Rustom Mody <rustompmody@gmail.com>
> wrote:
>> +1 for the slice in succinct form
> 
> Not only more succinct but also more correct. The purpose of islice is to
> slice arbitrary iterables as opposed to just sequences. But this function
> requires a reentrant iterable anyway and returns garbage if you pass it an
> iterator, so there's really no reason for it here. The version using slice
> notation on the other hand will raise a TypeError if you pass it an
> iterator or anything else that can't be sliced, which is preferable to
> silently returning incorrect results.

The natural remedy to that problem is of course itertools.tee():

>>> from itertools import islice, tee
>>> def n_grams(items, n):
...     z = (islice(it, start, None) for start, it in enumerate(tee(items, n)))
...     return zip(*z)
... 
>>> for item in n_grams(iter("abcde"), 3):
...     print("".join(item))
... 
abc
bcd
cde
>>> 

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: learning to use iterators Vito De Tullio <vito.detullio@gmail.com> - 2014-12-24 16:12 +0100
  Re: learning to use iterators Rustom Mody <rustompmody@gmail.com> - 2014-12-24 12:34 -0800
    Re: learning to use iterators Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-24 22:36 -0700
    Re: learning to use iterators Peter Otten <__peter__@web.de> - 2014-12-25 11:34 +0100

csiph-web