Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: What use of 'sum' in this line code? Date: Mon, 04 Jan 2016 01:53:51 +0100 Organization: None Lines: 53 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de rW1JJ5S/lD4Ck1/5ZLVWigTGcDVf7bCkqUzHcFbmiDoQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'resulting': 0.04; 'line:': 0.07; "subject:' ": 0.07; 'subject:code': 0.07; '22,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'snippet': 0.09; 'python': 0.10; 'applies': 0.15; '0.3': 0.16; '2],': 0.16; '33,': 0.16; 'numpy': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'import': 0.24; 'url:edu': 0.24; 'header:User-Agent:1': 0.26; 'developers': 0.26; 'header:X-Complaints-To:1': 0.26; 'values': 0.28; 'random': 0.29; 'code': 0.30; 'skip:[ 10': 0.31; 'source': 0.33; 'list': 0.34; 'could': 0.35; 'list:': 0.35; 'replace': 0.35; 'robert': 0.35; 'subject:use': 0.35; 'but': 0.36; 'list,': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'hi,': 0.38; 'from:': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'sum': 0.69; '0.8': 0.84; 'subject:this': 0.85; 'url:people': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd9b65.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101223 Robert wrote: > Hi, > > I find below code snippet on line: > > > ////////// > m = 10 > theta_A = 0.8 > theta_B = 0.3 > theta_0 = [theta_A, theta_B] > > coin_A = bernoulli(theta_A) > coin_B = bernoulli(theta_B) > > xs = map(sum, [coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m), coin_A.rvs(m), > coin_B.rvs(m)]) ///////// > > I see > [coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m), coin_A.rvs(m), > [coin_B.rvs(m)] > > is simply a list, but I don't know what use of 'sum' in this line. > I replace the random number with a simple list: > /////// > yy=map(sum, [13, 22, 33, 41]) > > In [24]: yy > Out[24]: [13, 22, 33, 41] > /////// > > I don't see 'sum' has any effect above. > The code source is from: > #http://people.duke.edu/~ccc14/sta-663/EMAlgorithm.html > > > What could you help me on the 'sum'? >>> import numpy >>> values = [13, 22, 33, 41] >>> map(numpy.sum, values) [13, 22, 33, 41] >>> values2 = [[1, 2], [3, 4]] >>> map(numpy.sum, values2) [3, 7] In Python 2 map(sum, values) applies sum to every value in the list and returns the resulting list of sums. Apparently the numpy developers found it convenient that sum(scalar) == scalar holds.