Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.038 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; '[1,': 0.09; 'calculating': 0.09; 'integers': 0.09; 'received:209.85.219': 0.09; 'python': 0.11; 'def': 0.12; 'elem': 0.16; 'guess.': 0.16; 'integers.': 0.16; 'skip:3 30': 0.16; 'skip:6 30': 0.16; 'skip:[ 30': 0.16; 'subject:when': 0.16; 'wow,': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'seems': 0.21; '>>>': 0.22; 'example': 0.22; 'email addr:gmail.com>': 0.22; 'to:name:python-list@python.org': 0.22; '"you': 0.24; '>>>': 0.24; 'example.': 0.24; '>': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'point': 0.28; 'correct': 0.29; 'subject:) ': 0.29; "doesn't": 0.30; 'skip:@ 10': 0.30; 'message-id:@mail.gmail.com': 0.30; 'that.': 0.31; 'lists': 0.32; 'worked': 0.33; 'fri,': 0.33; 'actual': 0.34; 'subject:from': 0.34; 'received:209.85': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'doing': 0.36; 'thanks': 0.36; 'skip:- 20': 0.37; 'list': 0.37; 'received:209': 0.37; 'skip:& 10': 0.38; 'lists.': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'realize': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'negative': 0.60; 'skip:n 30': 0.60; 'show': 0.63; 'such': 0.63; 'skip:n 10': 0.64; 'skip:1 20': 0.65; '.....': 0.78; 'as:': 0.81; 'subject:get': 0.81; 'confusing': 0.84; 'max,': 0.84; 'skip:8 30': 0.84; 'on?': 0.91; 'notebook': 0.96; '2013': 0.98 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:x-gm-message-state; bh=//6mdye62L1bie3qh7+vsbbV5hlumcKuPlb6IMC26T0=; b=jOoV/KvsupVKuVU/feAmW8/lz8kpldF5qzrgITrKadXKMCjWuE3MMOErzxzx6g11/d iYqXTtGoWH9/nJGMJx/X6B7ea69YcdAqEjD/WPbCAuS3D0qHKjT23b6CAJRnXHRHZBqT FJXSO88t/hZUoWjQnEkyhihSu21A45HfJSxjT0zdKBJz7NDRFMNNATGN1alBvi0g/voz T8YWspm7DtXRuyBpghjlJwzIh2V9iVqOiUs1+6VUppi25RJpyKgp+jg8i1wfOSSLdPX/ ydnKApjK6HmAx1W9DWVnst7fuooBH+mBguXwUViGv3a5FudaIZfi+LGq16vcAaQP+4dB bG+Q== MIME-Version: 1.0 X-Received: by 10.182.196.1 with SMTP id ii1mr6561783obc.93.1372433195531; Fri, 28 Jun 2013 08:26:35 -0700 (PDT) In-Reply-To: References: Date: Fri, 28 Jun 2013 16:26:35 +0100 Subject: Re: ? get negative from prod(x) when x is positive integers From: Vincent Davis To: "python-list@python.org" Content-Type: multipart/alternative; boundary=089e015383e01916e304e038817c X-Gm-Message-State: ALoCoQk+ymxDCYd0TEs8c4sKhOrHEm6JCIvZwvorziNKjl8IQviU3/y2qX4AZWF7OGINaelrD2cb X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 215 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372433204 news.xs4all.nl 15942 [2001:888:2000:d::a6]:34233 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49379 --089e015383e01916e304e038817c Content-Type: text/plain; charset=UTF-8 @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 wrote: > On 28 June 2013 15:38, Vincent Davis 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 > --089e015383e01916e304e038817c Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
@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 answe= r 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 u= sing it "ipython notebook --pylab inline".

Thanks

Vincent Davis
720-301-3003


On Fri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775808, 2])
=C2=A0 =C2=A0 ... 0

You want to use something that doesn't such as:

=C2=A0 =C2=A0 def prod(iter):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 p =3D 1
=C2=A0 =C2=A0 =C2=A0 =C2=A0 for elem in iter:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 p *=3D elem
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return p

and then you get your correct products:

=C2=A0 =C2=A0 8002414661101704746694488837062656
=C2=A0 =C2=A0 3907429033741066770846918377472
=C2=A0 =C2=A0 682872717747345471717929714096013312

--089e015383e01916e304e038817c--