Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #54304
| References | <l19gdf$psh$1@dont-email.me> <roy-7F8454.09003417092013@news.panix.com> <l19kvs$m4q$1@dont-email.me> <20130917091744.0aa6c577@bigbox.christie.dr> |
|---|---|
| Date | 2013-09-17 10:32 -0400 |
| Subject | Re: Having both if() and for() statements in one liner |
| From | Joel Goldstick <joel.goldstick@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.74.1379428343.18130.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
On Tue, Sep 17, 2013 at 10:17 AM, Tim Chase <python.list@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=='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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Having both if() and for() statements in one liner Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-09-17 15:02 +0300
Re: Having both if() and for() statements in one liner Robert Kern <robert.kern@gmail.com> - 2013-09-17 13:52 +0100
Re: Having both if() and for() statements in one liner Roy Smith <roy@panix.com> - 2013-09-17 09:00 -0400
Re: Having both if() and for() statements in one liner Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-09-17 16:21 +0300
Re: Having both if() and for() statements in one liner Heiko Wundram <modelnine@modelnine.org> - 2013-09-17 15:46 +0200
Re: Having both if() and for() statements in one liner Tim Chase <python.list@tim.thechases.com> - 2013-09-17 09:17 -0500
Re: Having both if() and for() statements in one liner Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-17 10:32 -0400
Re: Having both if() and for() statements in one liner Dave Angel <davea@davea.name> - 2013-09-17 18:54 +0000
Re: Having both if() and for() statements in one liner Steven D'Aprano <steve@pearwood.info> - 2013-09-18 02:10 +0000
Re: Having both if() and for() statements in one liner Joshua Landau <joshua@landau.ws> - 2013-09-18 06:55 +0100
Re: Having both if() and for() statements in one liner Chris Angelico <rosuav@gmail.com> - 2013-09-18 17:00 +1000
Re: Having both if() and for() statements in one liner Chris Angelico <rosuav@gmail.com> - 2013-09-18 01:04 +1000
Re: Having both if() and for() statements in one liner Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-17 11:15 -0400
csiph-web