[Tutor] summing arrays, along a dimension

Ricardo Aráoz ricaraoz at gmail.com
Sat Sep 15 01:06:49 CEST 2007


John wrote:
>>>> d
> array([[0, 0, 1],
>        [1, 2, 3],
>        [2, 2, 4],
>        [3, 6, 8]])
>>>> e=reshape((d[:,-2]+d[:,-1]),(4,1))
>>>> e
> array([[ 1],
>        [ 5],
>        [ 6],
>        [14]])
>  
> is there a better way to accomplish this?
> 

>>> d
[[0, 0, 1], [1, 2, 3], [2, 2, 4], [3, 6, 8]]
>>> e = [sum(i) for i in d]
>>> e
[1, 6, 8, 17]
>>> f = [sum([L[i] for L in d]) for i in xrange(len(d[0]))]
>>> f
[6, 10, 16]


More information about the Tutor mailing list