Python sum function - requires explanation of the `start` parameter

I am trying to understand how the built-in sum() function works, but the start parameter has evaporated:

  •  a=[[1, 20], [2, 3]] b=[[[[[[1], 2], 3], 4], 5], 6] >>> sum(b,a) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate list (not "int") to list >>> sum(a,b) [[[[[[1], 2], 3], 4], 5], 6, 1, 20, 2, 3] 
  •  >>> a=[1,2] >>> b=[3,4] >>> sum(a,b) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate list (not "int") to list >>> sum(b,a) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate list (not "int") to list 

I am simply stunned by this and do not know what is happening. Here's what python docs have to say: http://docs.python.org/library/functions.html#sum . This gives no explanation, "what if the beginning is not a string, not an integer?"

+6
source share
3 answers

Sum does something like this

 def sum(values, start = 0): total = start for value in values: total = total + value return total 

sum([1,2],[3,4]) extends something like [3,4] + 1 + 2 , which you can see, trying to add numbers and lists together.

To use sum to create lists, the values ​​must be a list of lists, while start can be just a list. In your unsuccessful examples, you will see that the list contains at least some ints, and not all lists.

The usual case when you might consider using a sum with lists is to convert a list of lists to a list

 sum([[1,2],[3,4]], []) == [1,2,3,4] 

But actually you should not do this, because it will be slow.

+14
source
 a=[[1, 20], [2, 3]] b=[[[[[[1], 2], 3], 4], 5], 6] sum(b, a) 

This error has nothing to do with the start parameter. There are two elements in list b . One of them is [[[[[1], 2], 3], 4], 5] , the other is 6 , and the list and int cannot be added together.

 sum(a, b) 

This addition:

 [[[[[[1], 2], 3], 4], 5], 6] + [1, 20] + [2, 3] 

Which works fine (since you just add lists to lists).

 a=[1,2] b=[3,4] sum(a,b) 

This is an attempt to add [3,4] + 1 + 2 , which again is impossible. Similarly, sum(b,a) adds [1, 2] + 3 + 4 .

What if the beginning is not a string, but not an integer?

sum cannot sum lines. Cm:

 >>> sum(["a", "b"], "c") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: sum() can't sum strings [use ''.join(seq) instead] 
+4
source

One of the things that were hinted at, but not explicitly indicated in other answers, is that the start value determines the type for the return value and for the elements to be summed . Since by default start=0 , (and, of course, 0 is an integer), all elements in an iterable must be integers (or types with the __add__ method, which works with integers). Other examples mention concatenation lists:

( sum([[1,2],[3,4]], []) == [1,2,3,4] )

or timedate.timedelta objects:

( sum([timedelta(1), timedelta(2)], timedelta()) == timedelta(3) ).

Note that both examples pass an empty type object to iterable as a start parameter to avoid a TypeError: unsupported operand type(s) for +: 'int' and 'list' error TypeError: unsupported operand type(s) for +: 'int' and 'list' .

+1
source

Source: https://habr.com/ru/post/924191/


All Articles