Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Loops and things

0 views
Skip to first unread message

rocco...@gmail.com

unread,
Dec 14, 2007, 10:25:42 AM12/14/07
to
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.

rocco...@gmail.com

unread,
Dec 14, 2007, 10:28:09 AM12/14/07
to

Bruno Desthuilliers

unread,
Dec 14, 2007, 10:38:34 AM12/14/07
to
rocco...@gmail.com a écrit :

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

Gary Herron

unread,
Dec 14, 2007, 10:52:12 AM12/14/07
to rocco...@gmail.com, pytho...@python.org
You can zip two ranges/iterators to produce successive tuples with
values from each iterator respectively, and loop on that:

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

Tim Chase

unread,
Dec 14, 2007, 10:57:29 AM12/14/07
to rocco...@gmail.com, pytho...@python.org
> 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

Neil Cerutti

unread,
Dec 14, 2007, 11:01:24 AM12/14/07
to

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

i.pa...@gmail.com

unread,
Dec 14, 2007, 3:42:46 PM12/14/07
to
On Dec 14, 5:01 pm, Neil Cerutti <horp...@yahoo.com> wrote:

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.

Jair Trejo

unread,
Dec 14, 2007, 4:06:51 PM12/14/07
to pytho...@python.org
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.

----------

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/

ZeD

unread,
Dec 15, 2007, 7:28:56 AM12/15/07
to
Jair Trejo wrote:

> 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

0 new messages