Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > conditional for-statement

Reply
Thread Tools

conditional for-statement

 
 
seb
Guest
Posts: n/a
 
      08-23-2009
Hi,

i was wondering if there is a syntax alike:

for i in range(10) if i > 5:
print i

equivalent to

for i in (for i in range(10) if i>5):
print i

sebastien
 
Reply With Quote
 
 
 
 
Benjamin Peterson
Guest
Posts: n/a
 
      08-23-2009
seb <sdementen <at> gmail.com> writes:

>
> Hi,
>
> i was wondering if there is a syntax alike:
>
> for i in range(10) if i > 5:
> print i


for i in range(10):
if i > 5:
print i




 
Reply With Quote
 
 
 
 
Francesco Bochicchio
Guest
Posts: n/a
 
      08-23-2009
On Aug 23, 10:09*am, seb <(E-Mail Removed)> wrote:
> Hi,
>
> i was wondering if there is a syntax alike:
>
> for i in range(10) if i > 5:
> * * print i
>
> equivalent to
>
> for i in (for i in range(10) if i>5):
> * * print i
>
> sebastien


AFAIK, no syntax fo that. But the standard syntax is not too
different:

for i in range(0):
if i > 5 : print i

Or you can use itertools.ifilter:

for i in itertools.ifilter( lambda x: x > 5, range(10) ):
print i

Or, if you define a function corresponding to the loop body, you could
do something like:

map( print, (i for i in range(10) if i> 5 )) # only works if print is
a function



Ciao
----
FB



 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      08-24-2009
seb a écrit :
> Hi,
>
> i was wondering if there is a syntax alike:
>
> for i in range(10) if i > 5:
> print i
>
> equivalent to
>
> for i in (for i in range(10) if i>5):
> print i


what about :

for i in range(6, 10):
print i

<g>


More seriously:

for i in range(10):
if i > 5:
print i
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
Preprocessor conditional compilation variable not being saved Chris P ASP .Net 0 10-28-2003 08:48 PM
Comparison of Bit Vectors in a Conditional Signal Assignment Statement Anand P Paralkar VHDL 2 08-04-2003 08:40 PM
Help: conditional attribute assignment itsme VHDL 1 07-23-2003 03:26 PM
Conditional signal declaration Willem Oosthuizen VHDL 5 07-17-2003 03:25 PM



Advertisments