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

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


Manuel Metz wrote:
> Chris Withers wrote:
>> Hi All,
>>
>> 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?
>>
>> My only solution so far is this:
>>
>> sum = arrays[0]
>> for a in arrays[1:]:
>>    sum += a
>>
>> ...which is ugly :-S
>>
>> cheers,
>>
>> Chris
> 
> sum = sum(array(sum(a) for a in arrays]))

Ah, sorry, typo....

sum = numpy.sum(numpy.array([numpy.sum(a) for a in arrays]))

and numpy.sum for clarity ...


More information about the Numpy-discussion mailing list