python for with double test

B

borges2003xx

hi all
the is a way for doing a for with double test:
example
for i in range(0,10) and f==1:
........


thanx everyone
 
P

Peter Hansen

hi all
the is a way for doing a for with double test:
example
for i in range(0,10) and f==1:
........

Not sure if you're asking a question, but is this what you are trying to
do? :

if f == 1:
for i in range(0,10):
...

If not, please explain more carefully what you want, maybe with an
example showing more than "..." in the body so we'll understand better.

-Peter
 
A

Alan Franzoni

Il 30 Dec 2005 09:02:30 -0800, (e-mail address removed) ha scritto:
hi all
the is a way for doing a for with double test:

what's a 'double test' exactly? :)

'for' does no test, it just iterates over a list. If you want to execute
the iteration only if f is 1, do this:

if f==1:
for i in range(0,10):
...

if you want to use only certain values in the range, when the value of f
(f may be a list, a dictionary, or whatever) is 1, you can use a list
comprehension:

for i in [x for x in range(0,10) if f[x]==1]:
....

of course, if you want to make it shorter, you can define a function
instead of the 'for' code block and use the list comprehension directly:

def myfunction(x):
...


[myfunction(x) for x in range(0,10) if f[x]==1]


and you'll end up with a list of the return values of myfunction.

--
Alan Franzoni <[email protected]>
-
Togli .xyz dalla mia email per contattarmi.
To contact me, remove .xyz from my email address.
-
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,372
Members
48,064
Latest member
alibsskemoSeAve

Latest Threads

Top