Is there a list comprehension for this?
Robert Kern
robert.kern at gmail.com
Tue Nov 21 17:41:03 EST 2006
liam_herron wrote:
> Given:
> dw = [ 1, -1.1, +1.2 ]
>
> Suppose I want to create a list 'w' that is defined as
>
> w[0] = dw[0],
> w[1] = w[0] + dw[1],
> w[2] = w[1] + dw[2]
>
> Is there a list comprehension or map expression to do it in one or 2
> lines.
One way is to use numpy (numpy.scipy.org):
In [40]: from numpy import cumsum
In [41]: dw = [1, -1.1, +1.2]
In [42]: cumsum(dw)
Out[42]: array([ 1. , -0.1, 1.1])
If you're doing a lot of numerical computing, you'll probably want a number of
other things that numpy provides.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list
mailing list