possibly trivial newbie list/array question

Alex Martelli aleax at aleax.it
Wed Aug 22 11:14:02 CEST 2001


"Michael Williams" <michael.williams at st-annes.ox.ac.uk> wrote in message
news:f3ccacbf.0108211646.28793074 at posting.google.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.  The built-in
array module has no direct support for per-item arithmetic, so it's
unlikely to be of much use to you.


Alex






More information about the Python-list mailing list