iterating over two sequences at once

D

Dan

This is relatively simple, borrowing from my C++/PHP background:

str = "abcdefg"
str2 = "abcde"

i = 0
for c in str:
print c, " ", str2
i += 2

But that is a C++/PHP way of doing things.
From what I've seen of python, I almost expected python to allow:

for c, c2 in str, str2:

But it didn't. I would be very surprised if there isn't another clever
way of doing the same though.

My question to you is, how would you do this? And do you think I should
propose a PEP for supporting the above for loop syntax? It seems like a
relatively harmless feature in that it won't break existing code, but
then I am very new to python.

Cheers,
-Dan
 
P

Peter Otten

Dan said:
This is relatively simple, borrowing from my C++/PHP background:

str = "abcdefg"
str2 = "abcde"

i = 0
for c in str:
print c, " ", str2
i += 2

But that is a C++/PHP way of doing things.
From what I've seen of python, I almost expected python to allow:

for c, c2 in str, str2:

But it didn't. I would be very surprised if there isn't another clever
way of doing the same though.

.... print c, d
....
a x
b y
c z

or, if i += 2 isn't there by mistake:
for c, d in zip("abc", "xyzwillbeignored"[::2]):
.... print c, d
....
a x
b z
c i
Using itertools.izip() instead of zip() doesn't create an intermediate list
of tuples which is an advantage when the zip/izip arguments are large
sequences.
My question to you is, how would you do this? And do you think I should
propose a PEP for supporting the above for loop syntax? It seems like a
relatively harmless feature in that it won't break existing code, but
then I am very new to python.

What you propose already has a meaning and therefore _will_ break existing
code.
.... print c, d
....
a b
c d

Peter
 
?

=?iso-8859-15?Q?Pierre-Fr=E9d=E9ric_Caillaud?=

for c, c2 in a, b:

This is a valid syntax but it does not do I think you think it does :

It will iterate over the list (in fact tuple) (a, b), so the loop will
run two times, just as if you had typed "for c, c2 in (a, b):".

At each iteration it will try to put the item in (c,c2), so if a and b
are 2-item tuples, say :
a= ("g",5) and b=("f",6)

you'll have :
first iteration :
c = "g" and c2=5
second iteration :
c = "f" and c2=6

got it ?

For can only manage one iterable (i'ts a one-level loop) so you'll either
need to use indices :

for pos, letter in enumerate(str1):
letter2 = str2[letter*2]
...

Or you can use zip to join your two iterables in one :
for letter1, letter2 in zip( str1, str2[::2]):
...

I'm not sure about the slice syntax [::2] but it means "every other
char", chekc the docs.
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top