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


Groups > comp.lang.python > #50464

Re: xslice idea | a generator slice

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.037
X-Spam-Evidence '*H*': 0.93; '*S*': 0.00; 'suppose': 0.07; 'iterate': 0.09; 'slices': 0.09; "wouldn't": 0.14; '2):': 0.16; 'example)': 0.16; 'generator.': 0.16; 'sequence.': 0.16; 'subject:generator': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'thu,': 0.19; 'print': 0.22; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'waste': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'noticed': 0.34; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'list': 0.37; 'sometimes': 0.38; 'to:addr:python- list': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'even': 0.60; 'expression': 0.60; 'new': 0.61; 'act': 0.63; 'myself': 0.63; 'more': 0.64; 'jul': 0.74; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=uYe7BPfTRqKKcL/ZAYzlsl4WNZ0ssqTYbNW3pEV9scY=; b=dTcJayzgHG94o0TvDxcKgucFOg0e6IsNNYKagzH6EoHH8h2/Ao8sfYucckMqm8sNrI +Ltp5EwOuRFUDRwCim8MHHk1ZMvNNIFwCzk+B1u8QI+nQxSqe6bKH3x92KJgV/dmNCue 72FyMY55uERDRbJ2SCbjGZDenCpPFUcNpcP2d1ofScbJxLbIddaG/SJ7xErDzTeFCY7D +7hrNA5PC4v58OyZKQy3vLPX9b2nyYxKhA08JyBclIEcQ1S2qCcJYjjtafvgodOCQp94 frdeqo+9NrrWqMm4Wd2N+aBqZ/4Vm4nMB8l6PWeO/OX5Q4ZnF9V0UYJkfI1CO8bWtNHF t6sQ==
X-Received by 10.66.164.232 with SMTP id yt8mr39342017pab.21.1373559438363; Thu, 11 Jul 2013 09:17:18 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <4a81b6a6-023e-4d47-9bd0-bbc0516caf6b@googlegroups.com>
References <4a81b6a6-023e-4d47-9bd0-bbc0516caf6b@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Thu, 11 Jul 2013 10:16:38 -0600
Subject Re: xslice idea | a generator slice
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4596.1373559441.3114.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1373559441 news.xs4all.nl 15926 [2001:888:2000:d::a6]:35996
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:50464

Show key headers only | View raw


On Thu, Jul 11, 2013 at 8:52 AM, Russel Walker <russ.pobox@gmail.com> wrote:
> Just some dribble, nothing major.
>
> I like using slices but I also noticed that a slice expression returns a new sequence.
>
> I sometimes find myself using them in a for loop like this:
>
>
> seq = range(10)
> for even in seq[::2]:
>     print even
>
>
> (That's just for an example) But wouldn't it be a bit of a waste if the slice expression returns a whole new list just when all you want to do in this case is iterate over it once?
>
> I suppose you could get around that will a little more code like:
>
>
> seq = range(10)
> for x in xrange(0, len(seq), 2):
>     print seq[x]
>
>
> But it would be nice there was a way to iterate over a 'slice' of sorts, that would act as a generator.

That would be the itertools.islice function.

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


Thread

xslice idea | a generator slice Russel Walker <russ.pobox@gmail.com> - 2013-07-11 07:52 -0700
  Re: xslice idea | a generator slice Russel Walker <russ.pobox@gmail.com> - 2013-07-11 07:54 -0700
    Re: xslice idea | a generator slice Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-07-11 16:14 +0100
      Re: xslice idea | a generator slice Russel Walker <russ.pobox@gmail.com> - 2013-07-11 09:21 -0700
        Re: xslice idea | a generator slice Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-07-11 17:34 +0100
        Re: xslice idea | a generator slice Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-11 12:00 -0600
        Re: xslice idea | a generator slice Fábio Santos <fabiosantosart@gmail.com> - 2013-07-11 20:58 +0100
        Re: xslice idea | a generator slice Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-11 15:02 -0600
  Re: xslice idea | a generator slice Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-11 10:16 -0600

csiph-web