possibly trivial newbie list/array question

Michael Williams michael.williams at st-annes.ox.ac.uk
Wed Aug 22 19:19:07 CEST 2001


"Alex Martelli" <aleax at aleax.it> wrote in message news:<9lvt4s018c6 at enews3.newsguy.com>...
> > (Amongst other things!) what I want to do is multiply an array of
> > floating points numbers with a float elementwise, i.e.
> >
> > >>> [1.0, 2.0, 3.0] * 4.0
> > [4.0, 8.0, 12.0]
> 
> The pure-Python way is a list-comprehension:
>     [x * 4.0 for x in [1.0, 2.0, 3.0]]
> [4.0, 8.0, 12.0]
.
> > relatively small (~10^2 elements). I looked at NumPy's array() object
> > and the array module that comes with a standard installation as
> > solutions and they both seem to fit the bill too. Is using either of
> > these an unecessariy complex/slow way of doing this (and only this)?
> 
> NumPy is VERY fast and excellent for your purposes.  

Thanks for the advice everyone. I was worried that using Numpy would
be a bit of sledghammer/nut for what is a relatively simple operation
but having done an array() it does seem to be the easiest way to
forget about the problem and have the array operate as I would like
them to in most circumstances. I'm probably worrying about speed a bit
to much becuase the actual maths of the method are inherently
inefficient. Numpy seems to offer readability as well as speed which
will do me just fine.

-- 
Michael Williams



More information about the Python-list mailing list