[SciPy-user] Avoiding For Loops Question

Whitcomb, Mr. Tim tim.whitcomb at nrlmry.navy.mil
Fri Jun 5 14:02:17 EDT 2009


> Tim,
> Just out of curiosity, why:
> 
> > i = numpy.arange(len(x))
> > a[:] = i[:]*(i[:]+1)/2*x[:]
> 
> and not:
> 
> > i = numpy.arange(len(x))
> > a = i*(i+1)/2*x
> 
> Ivo
> 

Because I'm used to doing this in some of my Fortran 90 code, and I
wasn't sure what the "right" way is to do it in Numpy/Scipy.  See
Josef's notes on this thread - he writes "[t]o write flexible code it is
better to leave [the [:] parts] off if they are not necessary."  Given
that, the second version is the "better" of the functionally equivalent
pair.

Tim



More information about the SciPy-User mailing list