[Tutor] summing arrays, along a dimension
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? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] summing arrays, along a dimension
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] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] summing arrays, along a dimension
John [EMAIL PROTECTED] 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. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] summing arrays, along a dimension
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? Which module are you using? In APL we'd write +/d to reduce along the rows using +. Does the array object have a similar reduce method? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor