# [Numpy-discussion] sum and prod

eat e.antero.tammi at gmail.com
Sat Sep 8 18:30:40 EDT 2012

```Hi,

On Sun, Sep 9, 2012 at 12:56 AM, nicky van foreest <vanforeest at gmail.com>wrote:

> Hi,
>
> I ran the following code:
>
>         args = np.array([4,8])
>         print np.sum( (arg > 0) for arg in args)
>         print np.sum([(arg > 0) for arg in args])
>         print np.prod( (arg > 0) for arg in args)
>         print np.prod([(arg > 0) for arg in args])
>
Can't see why someone would write code like above, but anyway:
In []: args = np.array([4,8])
In []: print np.sum( (arg > 0) for arg in args)
2
In []: print np.sum([(arg > 0) for arg in args])
2
In []: print np.prod( (arg > 0) for arg in args)
<generator object <genexpr> at 0x062BDA08>
In []: print np.prod([(arg > 0) for arg in args])
1
In []: print np.prod( (arg > 0) for arg in args).next()
True
In []: sys.version
Out[]: '2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]'
In []: np.version.version
Out[]: '1.6.0'

My 2 cents,
-eat

>
> with this result:
>
> 2
> 1
> <generator object <genexpr> at 0x1c70410>
> 1
>
> Is the difference between prod and sum intentional? I would expect
> that  numpy.prod would also work on a generator, just like numpy.sum.
>
> BTW: the last line does what I need: the product over the truth values
> of all elements of args. Is there perhaps a nicer (conciser) way to
> achieve this?  Thanks.
>
> Nicky
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120909/80b74318/attachment.html>
```