[SciPy-user] Avoiding For Loops Question

Joseph Smidt josephsmidt at gmail.com
Fri Jun 5 12:15:50 EDT 2009


Hi,

    I know that avoiding large for loops is a good way to speed up
your program.  I know you can avoid a for loop like this:

for i in xrange(len(x)):
     a[i] = 2.0*x[i]

as:

a[:] = 2.0*x[:]

   Now, is it possible to write get around these types of for loops
using any tools from scipy?

for i in xrange(len(x)):
     a[i] = i*(i+1)/2*x[i]

or this:

for i in xrange(y.shape[0]):
    for k in xrange(y.shape[1]):
        a[i] += x[i] + y[i][k]

or similarly:

for i in xrange(y.shape[0]):
    for k in xrange(y.shape[1]):
        a[i][k] = x[i] + y[i][k]

    Thanks.

                              Joseph Smidt


-- 
------------------------------------------------------------------------
Joseph Smidt <josephsmidt at gmail.com>

Physics and Astronomy
4129 Frederick Reines Hall
Irvine, CA 92697-4575
Office: 949-824-3269



More information about the SciPy-User mailing list