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


Groups > comp.lang.python > #54284 > unrolled thread

Having both if() and for() statements in one liner

Started byFerrous Cranus <nikos.gr33k@gmail.com>
First post2013-09-17 15:02 +0300
Last post2013-09-17 11:15 -0400
Articles 13 — 10 participants

Back to article view | Back to comp.lang.python


Contents

  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

#54284 — Having both if() and for() statements in one liner

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-09-17 15:02 +0300
SubjectHaving both if() and for() statements in one liner
Message-ID<l19gdf$psh$1@dont-email.me>
o want to avoid having to type somehting like this:

if person="George":
	times in range(0, 5):


Why it gives me an error when i'm trying to write it like this:


if person="George" for times in range(0, 5):

Can't i ahve both if and for in a one liner?

[toc] | [next] | [standalone]


#54292

FromRobert Kern <robert.kern@gmail.com>
Date2013-09-17 13:52 +0100
Message-ID<mailman.67.1379422507.18130.python-list@python.org>
In reply to#54284
On 2013-09-17 13:02, Ferrous Cranus wrote:
> o want to avoid having to type somehting like this:
>
> if person="George":
>      times in range(0, 5):
>
>
> Why it gives me an error when i'm trying to write it like this:
>
>
> if person="George" for times in range(0, 5):
>
> Can't i ahve both if and for in a one liner?

Not in Python, no.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

[toc] | [prev] | [next] | [standalone]


#54293

FromRoy Smith <roy@panix.com>
Date2013-09-17 09:00 -0400
Message-ID<roy-7F8454.09003417092013@news.panix.com>
In reply to#54284
In article <l19gdf$psh$1@dont-email.me>,
 Ferrous Cranus <nikos.gr33k@gmail.com> wrote:

> o want to avoid having to type somehting like this:
> 
> if person="George":
> 	times in range(0, 5):
> 
> 
> Why it gives me an error when i'm trying to write it like this:
> 
> 
> if person="George" for times in range(0, 5):

Step One when reporting a problem is don't just tell us you got an 
error.  Tell us what the error is.  Cut and paste the exact text of the 
full error message.

Although, in this case, it's pretty easy to guess that it was a syntax 
error :-)

I'm not sure where to start.  First, the '=' in 'person="George"' should 
be '=='.  In Python, '=' is used for assignment, '==' is used for 
equality testing.

Next, if you want to use the 1-line version of 'if', you need a ':' 
after the condition.  Something like:

    if person == 'George': print 'foo'

but it's generally considered poor style to use 1-line if statements.  
Just write it on two lines:

    if person == 'George':
        print 'foo'

They just discovered a huge newline vein in Montana and they're mining 
the things like crazy.  There's no shortage of them so feel free to use 
as many as you like.  They even get recycled.

But, I'm not even sure you can put a 'for' statement as the body of a 
1-line 'if'.  I've never tried it before, and my one experiment now got 
me a syntax error.  Even if it turns out to be legal and I just haven't 
got the details right, it's just The Wrong Thing To Do.

[toc] | [prev] | [next] | [standalone]


#54296

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-09-17 16:21 +0300
Message-ID<l19kvs$m4q$1@dont-email.me>
In reply to#54293
Στις 17/9/2013 4:00 μμ, ο/η Roy Smith έγραψε:
> In article <l19gdf$psh$1@dont-email.me>,
>   Ferrous Cranus <nikos.gr33k@gmail.com> wrote:
>
>> o want to avoid having to type somehting like this:
>>
>> if person="George":
>> 	times in range(0, 5):
>>
>>
>> Why it gives me an error when i'm trying to write it like this:
>>
>>
>> if person="George" for times in range(0, 5):
>
> Step One when reporting a problem is don't just tell us you got an
> error.  Tell us what the error is.  Cut and paste the exact text of the
> full error message.
>
> Although, in this case, it's pretty easy to guess that it was a syntax
> error :-)
>
> I'm not sure where to start.  First, the '=' in 'person="George"' should
> be '=='.  In Python, '=' is used for assignment, '==' is used for
> equality testing.
>
> Next, if you want to use the 1-line version of 'if', you need a ':'
> after the condition.  Something like:
>
>      if person == 'George': print 'foo'
>
> but it's generally considered poor style to use 1-line if statements.
> Just write it on two lines:
>
>      if person == 'George':
>          print 'foo'
>
> They just discovered a huge newline vein in Montana and they're mining
> the things like crazy.  There's no shortage of them so feel free to use
> as many as you like.  They even get recycled.
>
> But, I'm not even sure you can put a 'for' statement as the body of a
> 1-line 'if'.  I've never tried it before, and my one experiment now got
> me a syntax error.  Even if it turns out to be legal and I just haven't
> got the details right, it's just The Wrong Thing To Do.
>


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.

[toc] | [prev] | [next] | [standalone]


#54299

FromHeiko Wundram <modelnine@modelnine.org>
Date2013-09-17 15:46 +0200
Message-ID<mailman.70.1379425575.18130.python-list@python.org>
In reply to#54296
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 17.09.2013 15:21, schrieb Ferrous Cranus:
> ... there must be written on soem way.

You've already given yourself the answer in the initial post. The
Python way to write this is:

if person == "George":
    for times in range(5):
        ...

Why not just use what works and get some actual work done?

- -- 
- --- Heiko.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.20 (MingW32)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJSOF0gAAoJEDMqpHf921/Sv0oH/AyuaOk5sFlx4j7CKzv4Bb9i
+REyAtLJXpgcziviFXjIbnPsNLtGqMU6yOgp9OV7LGwfn0mnZtmI+SoYp08t7G9U
3WSMC6BOCugg419EEMmf+Gkf4fWvv/aZYWBTd8MhyiJLsQ9R7Sg9LlGYheDQ6m+S
RwWpYSHYCaJu3iy2xBJ+8AqQjOqACcMREtW1Rt1uHiydO93Dn2Abm0XLq11psYeR
OV3sftEJ2EpMEcR4I/HLx95KWIh7wvQcZywTF9y+pe1uOnLrKW/1NdkUxNdkMofy
RBNOjYJjT9JAnB2UHI1wVtbipwSi4A4zIIYsE6exv4s1IjnInrVERdDOOlqjwzQ=
=rxPO
-----END PGP SIGNATURE-----

[toc] | [prev] | [next] | [standalone]


#54302

FromTim Chase <python.list@tim.thechases.com>
Date2013-09-17 09:17 -0500
Message-ID<mailman.73.1379427403.18130.python-list@python.org>
In reply to#54296
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



[toc] | [prev] | [next] | [standalone]


#54304

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-09-17 10:32 -0400
Message-ID<mailman.74.1379428343.18130.python-list@python.org>
In reply to#54296

[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

[toc] | [prev] | [next] | [standalone]


#54327

FromDave Angel <davea@davea.name>
Date2013-09-17 18:54 +0000
Message-ID<mailman.88.1379444117.18130.python-list@python.org>
In reply to#54296
On 17/9/2013 09: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.
>
>

untested:

for times in range(0, 5 if person=="George" else 0):

But i also greately prefer the canonical version.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#54346

FromSteven D'Aprano <steve@pearwood.info>
Date2013-09-18 02:10 +0000
Message-ID<52390bb0$0$29869$c3e8da3$5496439d@news.astraweb.com>
In reply to#54327
On Tue, 17 Sep 2013 18:54:51 +0000, Dave Angel wrote:

> for times in range(0, 5 if person=="George" else 0):


Oh that is evil. Truly evil.

Thank you, I will treasure that piece of code forever.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#54356

FromJoshua Landau <joshua@landau.ws>
Date2013-09-18 06:55 +0100
Message-ID<mailman.102.1379483791.18130.python-list@python.org>
In reply to#54346
On 18 September 2013 03:10, Steven D'Aprano <steve@pearwood.info> wrote:
> On Tue, 17 Sep 2013 18:54:51 +0000, Dave Angel wrote:
>
>> for times in range(0, 5 if person=="George" else 0):
>
>
> Oh that is evil. Truly evil.
>
> Thank you, I will treasure that piece of code forever.

range(person == "simon" and 5)

[toc] | [prev] | [next] | [standalone]


#54359

FromChris Angelico <rosuav@gmail.com>
Date2013-09-18 17:00 +1000
Message-ID<mailman.105.1379487609.18130.python-list@python.org>
In reply to#54346
On Wed, Sep 18, 2013 at 3:55 PM, Joshua Landau <joshua@landau.ws> wrote:
> range(person == "simon" and 5)

+1 for the BOFH reference.

ChrisA

[toc] | [prev] | [next] | [standalone]


#54310

FromChris Angelico <rosuav@gmail.com>
Date2013-09-18 01:04 +1000
Message-ID<mailman.77.1379430607.18130.python-list@python.org>
In reply to#54293
On Tue, Sep 17, 2013 at 11:00 PM, Roy Smith <roy@panix.com> wrote:
> They just discovered a huge newline vein in Montana and they're mining
> the things like crazy.  There's no shortage of them so feel free to use
> as many as you like.  They even get recycled.

Can they keep up with the considerable demand even from Whitespace?

http://en.wikipedia.org/wiki/Whitespace_(programming_language)

I hope the Montanan newlines aren't on silly US export restrictions.
That would be so annoying.

ChrisA

[toc] | [prev] | [next] | [standalone]


#54312

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-09-17 11:15 -0400
Message-ID<mailman.79.1379430956.18130.python-list@python.org>
In reply to#54293

[Multipart message — attachments visible in raw view] — view raw

On Tue, Sep 17, 2013 at 11:04 AM, Chris Angelico <rosuav@gmail.com> wrote:

> On Tue, Sep 17, 2013 at 11:00 PM, Roy Smith <roy@panix.com> wrote:
> > They just discovered a huge newline vein in Montana and they're mining
> > the things like crazy.  There's no shortage of them so feel free to use
> > as many as you like.  They even get recycled.
>
> Can they keep up with the considerable demand even from Whitespace?
>
> http://en.wikipedia.org/wiki/Whitespace_(programming_language)
>
> I hope the Montanan newlines aren't on silly US export restrictions.
> That would be so annoying.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>


You can get them all over the Mediterranean as well!  I think they are like
air --

-- 
Joel Goldstick
http://joelgoldstick.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web