Iteration over two sequences

John Lenton jlenton at gmail.com
Wed Jan 12 12:05:14 EST 2005


> Quite frequently, I find the need to iterate over two sequences at
the
> same time, and I have a bit of a hard time finding a way to do this
in a
> "pythonic" fashion. One example is a dot product. The straight-ahead
> C-like way of doing it would be:
>
> def dotproduct(a, b):
>    psum = 0
>    for i in range(len(a)):
>        psum += a[i]*b[i]
>    return psum

for this particular example, the most pythonic way is to do nothing at
all, or, if you must call it dotproduct,
>>> from Numeric import dot as dotproduct




More information about the Python-list mailing list