[Numpy-discussion] newbie question - summing a list of arrays

Manuel Metz mmetz@astro.uni-bonn...
Tue Mar 18 10:52:07 CDT 2008


Chris Withers wrote:
> Alan G Isaac wrote:
>> On Tue, 18 Mar 2008, Chris Withers apparently wrote:
>>> Say I have an aribtary number of arrays:
>>> arrays = [array([1,2,3]),array([4,5,6]),array([7,8,9])] 
>>> How can I sum these all together? 
>> Try N.sum(arrays,axis=0).
> 
> I assume N here is:
> 
> import numpy as N?
> 
> Yep, it is... and that works exactly as I expect.
> 
> Where are the docs for sum? Having had the book turn up as a massive PDF 
> with a poor index/toc, I'm finding it just as difficult to navigate as 
> the online docs :-(
> (I, like most people on this list I'd guess, sadly don't have the time 
> to sit and read the whole book cover-to-cover to extract the 10-20% I 
> need to know :-S)
> 
>> But must they be in a list?
>> An array of arrays (i.e., 2d array) is easy to sum.
> 
> Actually, I'm using a dict of arrays:
> 
> data = {
>   'series1':array([1,2,3]),
>   'series2':array([1,4,6]),
>    'date':array([datetime(...),datetime(...),datetime(...)]),
>   }
> 
> If that gives the idea?

Hm, in this case you can do it like this:

numpy.sum(numpy.array([numpy.sum(v) for k,v in data.items()]))

> Is there perhaps a better way to store these series?
> (I'm a numpy newbie, I've skimmed the tutorial and it doesn't appear to 
> help here)
> 
> cheers,
> 
> Chris
> 


More information about the Numpy-discussion mailing list