Loops and things

R

rocco.rossi

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);
}

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.
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
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);
}

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?

What's your use case exactly ? I mean, the *real* problem you're trying
to solve this way ?
 
G

Gary Herron

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);
}

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.
You can zip two ranges/iterators to produce successive tuples with
values from each iterator respectively, and loop on that:
.... 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
 
T

Tim Chase

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);
}

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
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top