possibly trivial newbie list/array question

Sylvain Thenault syt at pegasus.logilab.fr
Wed Aug 22 03:47:56 EDT 2001


On 21 Aug 2001, Michael Williams <michael.williams at st-annes.ox.ac.uk> wrote:
>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]
>

IMHO, the faster way to do it consists in using builtin map function:

map([1.0, 2.0, 3.0], lambda x: 4.0*x)

or, if you have Python > 2.0, you should use list comprehension:

l=[1.0, 2.0, 3.0]
[item*4.0 for item in l]

-- 
Sylvain Thenault

LOGILAB




More information about the Python-list mailing list