for x,y in word1, word2 ?

S

ssecorp

Is there a syntax for looping through 2 iterables at the same time?

for x in y:
for a in b:


is not what I want.

I want:
for x in y and for a in b:
 
S

ssecorp

Is there a syntax for looping through 2 iterables at the same time?
for x in y:
for a in b:
is not what I want.
I want:
for x in y and for a in b:

Something like this?
a = ['a','b','c']
b = [1,2,3]
zip(a,b)

[('a', 1), ('b', 2), ('c', 3)]

I know zip but lets say I have a word "painter" and I want to compare
it to a customer's spelling, he might have written "paintor" and I
want to check how many letters are the same.

Now I know how I could do this, it is not hard.
I am just wondering if these is any specific simple syntax for it.
 
J

Jon Clements

Is there a syntax for looping through 2 iterables at the same time?
for x in y:
for a in b:
is not what I want.
I want:
for x in y and for a in b:

Something like this?
a = ['a','b','c']
b = [1,2,3]
zip(a,b)

[('a', 1), ('b', 2), ('c', 3)]

I would have thought the difflib library and SequenceMatcher would do
(more than) what you're after.

Just an idea anyway,

Jon.
 
D

dbpokorny

Something like this?
a = ['a','b','c']
b = [1,2,3]
zip(a,b)
[('a', 1), ('b', 2), ('c', 3)]

I know zip but lets say I have a word "painter" and I want to compare
it to a customer's spelling, he might have written "paintor" and I
want to check how many letters are the same.

Now I know how I could do this, it is not hard.
I am just wondering if these is any specific simple syntax for it.

There are two answers: first, if your domain interest is spell-
checking, search for "phonetic algorithm" or "soundex python". If your
interest is iteration, check out itertools - AFAIK this is the closest
you will get to a simple syntax for iterating over the diagonal as
opposed to the Cartesian product.
... print x == y
...

David
 
M

Marc 'BlackJack' Rintsch

I know zip but lets say I have a word "painter" and I want to compare it
to a customer's spelling, he might have written "paintor" and I want to
check how many letters are the same.

Now I know how I could do this, it is not hard. I am just wondering if
these is any specific simple syntax for it.

No special syntax for that, but you can combine the `sum()` function, a
generator expression and `zip()`:

In [40]: sum(int(a == b) for a, b in zip('painter', 'paintor'))
Out[40]: 6

Or this way if you think it's more clear:

In [41]: sum(1 for a, b in zip('painter', 'paintor') if a == b)
Out[41]: 6

Ciao,
Marc 'BlackJack' Rintsch
 
R

Raja Baz

Is there a syntax for looping through 2 iterables at the same time?

for x in y:
� � for a in b:

is not what I want.

I want:
for x in y and for a in b:

Something like this?
a = ['a','b','c']
b = [1,2,3]
zip(a,b)
[('a', 1), ('b', 2), ('c', 3)]

zip and the nested loops don't do the same thing
zipping then comparing would compare items that are at the same position
the nested loops would compare each item to all the items in the other
sequence, big difference
AFAIK there's no better way to accomplish what the nested loops do with
a simpler syntax
 
P

Paul Rubin

ssecorp said:
for x in y and for a in b:

for x,a in zip(y,b): ...

or the iterator version:

from itertools import izip
for x,a in izip(y,b): ...

avoids allocating a list to iterate through.
 

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,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top