? get negative from prod(x) when x is positive integers

Vincent Davis vincent at vincentdavis.net
Fri Jun 28 11:26:35 EDT 2013


@Joshua
"You are using numpy.prod()"
Wow, since sum([1,2,3,4]) worked I tried prod([1,2,3,4]) and got the right
answer so I just used that. Confusing that it would use numpy.prod(), I
realize now there is no python prod(). At no point do I "import numpy" in
my code. The seems to be a result of using ipython, or at least how I am
using it "ipython notebook --pylab inline".

Thanks

Vincent Davis
720-301-3003


On Fri, Jun 28, 2013 at 4:04 PM, Joshua Landau
<joshua.landau.ws at gmail.com>wrote:

> On 28 June 2013 15:38, Vincent Davis <vincent at vincentdavis.net> wrote:
> > I have a list of a list of integers. The lists are long so i cant really
> > show an actual example of on of the lists, but I know that they contain
> only
> > the integers 1,2,3,4. so for example.
> > s2 = [[1,2,2,3,2,1,4,4],[2,4,3,2,3,1]]
> >
> > I am calculating the product, sum, max, min.... of each list in s2 but I
> get
> > negative or 0 for the product for a lot of the lists. (I am doing this in
> > ipython)
> >
> > for x in s2:
> >     print('len = ', len(x), 'sum = ', sum(x), 'prod = ', prod(x), 'max =
> ',
> > max(x), 'min = ', min(x))
> >
> > ...
> >
> > ('len = ', 100, 'sum = ', 247, 'prod = ', 0, 'max = ', 4, 'min = ', 1)
> > ('len = ', 100, 'sum = ', 230, 'prod = ', -4611686018427387904, 'max =
> ', 4,
> > 'min = ', 1)
> > ('len = ', 100, 'sum = ', 261, 'prod = ', 0, 'max = ', 4, 'min = ', 1)
> >
> > .....
> >
> > ('prod =', 0, 'max =', 4, 'min =', 1)
> > ('prod =', 1729382256910270464, 'max =', 4, 'min =', 1)
> > ('prod =', 0, 'max =', 4, 'min =', 1)
> >
> > ....
> >
> >
> > Whats going on?
>
> Let me guess.
> These are your lists (sorted):
>
> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
> 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
> 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
> 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
> 4, 4, 4, 4, 4, 4, 4, 4]
>
> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
> 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
> 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
> 4, 4, 4, 4, 4, 4, 4, 4]
>
> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
> 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3,
> 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4,
> 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
> 4, 4, 4, 4, 4, 4, 4, 4]
>
> You are using numpy.prod()
>
> Numpy.prod overflows:
>
>     >>> numpy.prod([-9223372036854775808, 2])
>     ... 0
>
> You want to use something that doesn't such as:
>
>     def prod(iter):
>         p = 1
>         for elem in iter:
>             p *= elem
>         return p
>
> and then you get your correct products:
>
>     8002414661101704746694488837062656
>     3907429033741066770846918377472
>     682872717747345471717929714096013312
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130628/7f9acd94/attachment.html>


More information about the Python-list mailing list