Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101223

Re: What use of 'sum' in this line code?

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: What use of 'sum' in this line code?
Date 2016-01-04 01:53 +0100
Organization None
Message-ID <mailman.1.1451868841.2305.python-list@python.org> (permalink)
References <b3f36467-331d-4262-868f-22583c33ba72@googlegroups.com>

Show all headers | View raw


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.


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

What use of 'sum' in this line code? Robert <rxjwg98@gmail.com> - 2016-01-03 16:28 -0800
  Re: What use of 'sum' in this line code? Steven D'Aprano <steve@pearwood.info> - 2016-01-04 11:43 +1100
  Re: What use of 'sum' in this line code? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-01-04 00:48 +0000
  Re: What use of 'sum' in this line code? Peter Otten <__peter__@web.de> - 2016-01-04 01:53 +0100
    Re: What use of 'sum' in this line code? Robert <rxjwg98@gmail.com> - 2016-01-03 18:44 -0800

csiph-web