Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intended effect of sum over a python map

sum(map(len, ["a", "aa", "aaa"])) gives me a map instead of a number.

I expected this to give the same result as sum(len(thing) for thing in ["a", "aa", "aaa"]) (that is 6).

I see that list(sum(map(len, ["a", "aa", "aaa"]))) returns me [1, 2, 3], as if the sum had no effect.

I assume there is a reason for such a behaviour. Is there an intended use case for this?

like image 297
bli Avatar asked Dec 31 '25 10:12

bli


1 Answers

It appears that I was working in ipython and that the builtin sum function was masked by "function sum in module numpy.core.fromnumeric" (according to help(sum)). I suspect an effect of having issued the command %pylab.

__builtin__.sum(map(len, ["a", "aa", "aaa"])) gives the expected 6.

like image 110
bli Avatar answered Jan 02 '26 23:01

bli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!