for (i = 0, j = 10; i < 10 && j < 20; i++, j++) {
printf("i = %d, j = %d\n", i, j);
}
So that I would get:
i = 0, j = 0
i = 1, j = 1
i = 2, j = 2
...
...
...
i = 9, j = 19
Can this be done in Python?
Thanks.
What's your use case exactly ? I mean, the *real* problem you're trying
to solve this way ?
>>> for i,j in zip(range(10),range(10,20)):
... print i,j
...
0 10
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
Gary Herron
Well, yes it can be done, but depending on your use-case, there
might be smarter ways of doing it:
for (i,j) in map(lambda i: (i, i+10), xrange(10)):
print "i = %d, j = %d" % (i,j)
or just
for pair in map(lambda i: (i, i+10), xrange(10)):
print "i = %d, j = %d" % pair
or even just
for i in xrange(10):
print "i = %d, j = %d" % (i,i+10)
If you need varying sources, you can use zip() to do something like
for (i,j) in zip(xrange(10), myiter(72)):
print "i = %d, j = %d" % (i,j)
where myiter() produces the random sequence of items for j.
If they produce voluminous output, you can import itertools and
use izip and imap instead.
Or, if you want a more literal mapping:
i, j = 0, 10
while i < 10 && j < 20:
print "i = %d, j = %d" % (i,j)
i += 1
j += 1
Pick your poison.
> So that I would get:
>
> i = 0, j = 0
> i = 1, j = 1
> i = 2, j = 2
> ...
> ...
I'm not sure how, with your code, "j" could be (0,1,2,...)
instead of (10,11,12,...).
-tkc
Yes, assuming you meant to say:
i = 0, j = 10
i = 0, j = 11
...
i = 9, j = 19
import sys
from itertools import izip
for i, j in izip(xrange(10), xrange(10, 20)):
sys.stdout.write("i = %d, j = %d\n", (i, j))
--
Neil Cerutti
To succeed in the world it is not enough to be stupid, you must also be well-
mannered. --Voltaire
Yeah, that's what I meant ... ooops :)
Thanks a lot to everyone for the useful info. In the meantime I had
found out about zip and that way of doing it. But I really appreciated
all the different alternative solutions that were illustrated,
especially the more "functional" ones with map ... very cool, I'm also
a big Lisp fan, and I really dig those.
for (i = 0, j = 10; i < 10 && j < 20; i++, j++) {
printf("i = %d, j = %d\n", i, j);
}
So that I would get:
i = 0, j = 0
i = 1, j = 1
i = 2, j = 2
...
...
...
i = 9, j = 19
Can this be done in Python?
Thanks.
----------
In your case, j is simply i+10; so can just write
j=i+10 inside the loop, or even use i+10 itself.
____________________________________________________________________________________
ĄCapacidad ilimitada de almacenamiento en tu correo!
No te preocupes más por el espacio de tu cuenta con Correo Yahoo!:
http://correo.yahoo.com.mx/
> I was wondering how and if it's possible to write a
> loop in python
> which updates two or more variables at a time. For
> instance, something
> like this in C:
>
> for (i = 0, j = 10; i < 10 && j < 20; i++, j++) {
> printf("i = %d, j = %d\n", i, j);
> }
>>> for i,j in zip(range(0, 10), range(10, 20)):
... print("i = %d, j = %d" % (i, j))
...
i = 0, j = 10
i = 1, j = 11
i = 2, j = 12
i = 3, j = 13
i = 4, j = 14
i = 5, j = 15
i = 6, j = 16
i = 7, j = 17
i = 8, j = 18
i = 9, j = 19
>>>
--
Under construction