Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:both': 0.07; 'logic': 0.09; 'runtime': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '-tkc': 0.16; '10:17': 0.16; 'argument,': 0.16; 'evaluated.': 0.16; 'great!': 0.16; 'iterable': 0.16; 'iterable:': 0.16; 'nay': 0.16; 'range(0,': 0.16; 'tim,': 0.16; 'to:addr:python.list': 0.16; 'to:addr:tim.thechases.com': 0.16; 'to:name:tim chase': 0.16; 'wrote:': 0.18; 'written': 0.21; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; '>': 0.26; 'header :In-Reply-To:1': 0.27; 'am,': 0.29; 'tim': 0.29; 'said,': 0.30; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'url:wiki': 0.31; 'chase': 0.31; 'sep': 0.31; 'url:wikipedia': 0.31; 'run': 0.32; 'url:python': 0.33; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'subject:one': 0.36; 'yield': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'skip:& 10': 0.38; 'form,': 0.38; 'url:mail': 0.40; 'even': 0.60; 'cost.': 0.60; 'wonderful': 0.60; 'times': 0.62; 'world': 0.66; 'dont': 0.67; 'canonical': 0.91; 'joel': 0.91; '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:date:message-id:subject:from:to :cc:content-type; bh=LMUyiy1R1nsOn6qSmV3HP4PZJMQQThUFMgX3DuU3oSg=; b=afwS+7SwEYFBEyFQWsCqMFyNonNkfp/+jlCxzlfRKonTicqfDtv9FyOmWhJMnVQtsy rlrJODE3Gt9M6jzDjd4myyId2NVRZYDgOENotR26cvPDfJqy/yr7z69mjfWoQFrI07FK G/28hFYoTM5XCgojryZZO+Vvp4KTpT71AN7qFNqaB7UEmaQEgj3iLf6hVEXwe8fTx7Bm WaMlPKbrMUC+tto72R0PMeTDk/qlneKCByZpc5JLdGY3nBZ2uS/S2ZT3zkHiW3crQJkN 4sl7mOUM9tPeMg3HNj2cldc8Ayfbkc5yGBrsvu9msryP1H3zrM2x2Q3FkN2qCopkXfy9 SeJQ== MIME-Version: 1.0 X-Received: by 10.58.186.238 with SMTP id fn14mr88457vec.52.1379428340300; Tue, 17 Sep 2013 07:32:20 -0700 (PDT) In-Reply-To: <20130917091744.0aa6c577@bigbox.christie.dr> References: <20130917091744.0aa6c577@bigbox.christie.dr> Date: Tue, 17 Sep 2013 10:32:20 -0400 Subject: Re: Having both if() and for() statements in one liner From: Joel Goldstick To: Tim Chase Content-Type: multipart/alternative; boundary=047d7b6da8c837846704e6953084 Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 131 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1379428343 news.xs4all.nl 15992 [2001:888:2000:d::a6]:59500 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54304 --047d7b6da8c837846704e6953084 Content-Type: text/plain; charset=UTF-8 On Tue, Sep 17, 2013 at 10:17 AM, Tim Chase wrote: > On 2013-09-17 16:21, Ferrous Cranus wrote: > > I just want to say tot he program that > > > > that only run the for statement if and only if person=='George' > > > > I dont see nay reason as to why this fails > > > > perhaps like: > > > > for times in range(0, 5) if person=='George': > > > > but that fails too... > > there must be written on soem way. > > The canonical way to do this is the obvious: > > if person == "George": > for times in range(0, 5): > ... > > That said, you can do stupid things to abstract the logic like > > def iterate_if(condition, iterable): > if condition: > for item in iterable: > yield item > > which you can use something like > > for times in iterate_if(person == "George", range(0,5)): > ... > > but I don't advise it. Mainly, because the iterable will be > evaluated when passed as an argument, which incurs the runtime cost. > In the canonical form, if the test isn't passed, the range(n,m) is > never even evaluated. > > -tkc > > > Tim, that's great! or in the wonderful world of Onslow, "Oh nice" http://en.wikipedia.org/wiki/Onslow_(Keeping_Up_Appearances) > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick http://joelgoldstick.com --047d7b6da8c837846704e6953084 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable



On Tue, Sep 17, 2013 at 10:17 AM, Tim Chase <<= a href=3D"mailto:python.list@tim.thechases.com" target=3D"_blank">python.li= st@tim.thechases.com> wrote:
On 2013= -09-17 16:21, Ferrous Cranus wrote:
> I just want to say tot he program that
>
> that only run the for statement if and only if person=3D=3D'George= '
>
> I dont see nay reason as to why this fails
>
> perhaps like:
>
> for times in range(0, 5) if person=3D=3D'George':
>
> but that fails too...
> there must be written on soem way.

The canonical way to do this is the obvious:

=C2=A0 if person =3D=3D "George":
=C2=A0 =C2=A0 for times in range(0, 5):
=C2=A0 =C2=A0 =C2=A0 ...

That said, you can do stupid things to abstract the logic like

=C2=A0 def iterate_if(condition, iterable):
=C2=A0 =C2=A0 if condition:
=C2=A0 =C2=A0 =C2=A0 for item in iterable:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 yield item

which you can use something like

=C2=A0 for times in iterate_if(person =3D=3D "George", range(0,5)= ):
=C2=A0 =C2=A0 ...

but I don't advise it. =C2=A0Mainly, because the iterable will be
evaluated when passed as an argument, which incurs the runtime cost.
In the canonical form, if the test isn't passed, the range(n,m) is
never even evaluated.

-tkc


Tim, that's great! or in the wonder= ful world of Onslow, "Oh nice" =C2=A0 http://en.wikipedia.org/wiki/= Onslow_(Keeping_Up_Appearances)
=C2=A0


--
https://mail.python.org/mailman/listinfo/python-list



--
--047d7b6da8c837846704e6953084--