[Numpy-discussion] sum of positive values in an array

David Cournapeau david@ar.media.kyoto-u.ac...
Fri Sep 5 09:32:41 CDT 2008


Ludwig wrote:
> What are the relative merits of 
>
> sum(a[where(a>0])
>
> to 
>
> a[a > 0].sum()
>
> ? 
> Second one is more OO, takes a few keystrokes less to type. Is there
> any real difference if it came to very large arrays? Or is it 'just' a
> style question?

In this case, it is style: sum(a) really calls a.sum() internally; in
the second case, you are sure to call numpy sum, and not python sum,
which is a common mistake of people new to numpy.

cheers,

David


More information about the Numpy-discussion mailing list