[Tutor] summing arrays, along a dimension

Alan Gauld alan.gauld at btinternet.com
Sat Sep 15 01:34:02 CEST 2007


"John" <washakie at gmail.com> 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?

Better? Maybe. More readable I think is:

e1 = [ [row[-2]+row[-1]]  for row in d ]

You can convert the 2D list to an array object if you want later


Alan G.



More information about the Tutor mailing list