for x,y in word1, word2 ?

dbpokorny at gmail.com dbpokorny at gmail.com
Mon Aug 11 03:06:37 EDT 2008


On Aug 10, 11:14 pm, ssecorp <circularf... at gmail.com> wrote:
> On Aug 11, 6:40 am, Mensanator <mensana... at aol.com> wrote:
>
>
>
> > On Aug 10, 11:18 pm, ssecorp <circularf... at gmail.com> wrote:
>
> > > 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.

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.

>>> from itertools import *
>>> for x,y in izip('painter','paintor'):
...   print x == y
...

David



More information about the Python-list mailing list