possibly trivial newbie list/array question

Michael Williams michael.williams at st-annes.ox.ac.uk
Wed Aug 22 02:46:20 CEST 2001


Hi,

Sorry if this question has been done to death but my friend Mr Google
Groups wasn't able to point me in the direction of the answer.

I'm an undergraduate doing a relatively simple bit of functional
programming, looking at the behaviour of Lorenz attractors. My
department recommends (or uses as standard) Pascal. I'd rather avoid
this, and Python is something I've been looking into.

(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]

Of course this is not the usual behaviour of lists, which is to repeat
the list making a 12 object list in that example. The obvious way that
struck me to do this was simply to step through the list by index,
multiplying each element "manually", to wit:

array1 = [1.0, 2.0, 3.0]
for i in len(array1):
    array1[i] = array1[i] * 4.0

The program will do this operation a lot so I'm concerned that this
should be the most efficient way of doing this. Is it? My arrays are
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)?

Thanks for any advice,

-- 
Michael Williams



More information about the Python-list mailing list