Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'builtin': 0.09; 'calculating': 0.09; 'integers': 0.09; 'overflow': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'bug': 0.12; 'jan': 0.12; 'language.': 0.14; 'guessing': 0.16; 'integers,': 0.16; 'integers.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'res': 0.16; 'skip:[ 30': 0.16; 'subject:when': 0.16; 'underlying': 0.16; 'wrote:': 0.18; 'example': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'adds': 0.24; 'example.': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'am,': 0.29; 'subject:) ': 0.29; 'url:wiki': 0.31; 'url:wikipedia': 0.31; 'lists': 0.32; 'actual': 0.34; 'subject:from': 0.34; 'something': 0.35; 'but': 0.35; 'version': 0.36; 'really': 0.36; 'doing': 0.36; 'url:org': 0.36; 'should': 0.36; 'skip:- 20': 0.37; 'list': 0.37; 'lists.': 0.38; 'to:addr :python-list': 0.38; 'rather': 0.38; '(from': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'negative': 0.60; 'worry': 0.60; 'received:173': 0.61; 'you.': 0.62; 'show': 0.63; 'great': 0.65; 'below.': 0.71; 'subject:get': 0.81; '*really*': 0.84; '2.7.': 0.84; 'max,': 0.84; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: ? get negative from prod(x) when x is positive integers Date: Fri, 28 Jun 2013 11:30:35 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 In-Reply-To: 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372433452 news.xs4all.nl 15863 [2001:888:2000:d::a6]:38969 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49381 On 6/28/2013 10:38 AM, 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) Based on Python2 (from print output). Look at the underlying version to make sure it is 2.7. Using Python 3 or something based on it is better unless you *really* have to use Python 2. > for x in s2: > print('len = ', len(x), 'sum = ', sum(x), 'prod = ', prod(x), 'max > = ', max(x), 'min = ', min(x)) prod is not a Python builtin. I am guessing ipython adds it as a C-coded builtin because a Python-coded function* would not have the overflow bug exhibited below. See for instance https://en.wikipedia.org/wiki/Integer_overflow Not having to worry about this, because Python comes with multi-precision integers, is a great thing about using Python rather than almost any other language. * I do not remember it this was always true for old enough Pythons. > ('len = ', 100, 'sum = ', 247, 'prod = ', 0, 'max = ', 4, 'min = ', 1) > ('len = ', 100, 'sum = ', 230, 'prod = ', -4611686018427387904, 'max = ', 4, 'min = ', 1) def prod(seq): res=1 for i in seq: res *= i return res should work for you. -- Terry Jan Reedy