3 ms·
Python sum() can handle summing up other data structures, although, as with foldl, you may have to give it an initial accumulator argument: >>> import nump
by kragen 7y ago
Python sum() can handle summing up other data structures, although, as with foldl, you may have to give it an initial accumulator argument:
>>> import numpy as np
>>> xs = np.arange(9).reshape((3,3))
>>> xs
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> sum(xs) # builtin sum, not numpy sum
array([ 9, 12, 15])
>>> sum([[3, 11], [5]], [])
[3, 11, 5]
>>> sum([(3, 11), (5,)], ())
(3, 11, 5)
However, that doesn't work for strings; there's a special-case check for that.