[Python-ideas] Fast sum() for non-numbers

Alexander Belopolsky alexander.belopolsky at gmail.com
Thu Jul 11 22:08:44 CEST 2013


On Thu, Jul 11, 2013 at 2:29 PM, Ron Adam <ron3200 at gmail.com> wrote:

> I was asking how numpy it gets around sum() needing a starting 'array'
> argument?


When you sum arrays, you don't need to start with an array:

>>> import numpy
>>> 0 + numpy.array([1, 2, 3])
array([1, 2, 3])
>>> sum([_, _, _])
array([3, 6, 9])

The default scalar start gets broadcast to the shape of the array.

>>> sum([numpy.zeros((2,2))], 42)
array([[ 42.,  42.],
       [ 42.,  42.]])
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130711/21ab4ce8/attachment.html>


More information about the Python-ideas mailing list