I need the answer, I don't know why this output, and I need the explication of itertools | Sololearn: Learn to code for FREE!

+8

I need the answer, I don't know why this output, and I need the explication of itertools

https://code.sololearn.com/c7dJxwWk5RnT/?ref=app

5 Answers

New Answer

+9

The accumulate function in the itertools module, by default, will take in an iterable and returns an iterable that keeps a running sum of its items. The original range converted to a list would be; [0, 1, 2, 3, 4, 5, 6, 7] Where the accumulated list returned is; [0, 1, 3, 6, 10, 15, 21, 28] Notice if you start at the 1st element and then add the values of the previous elements that the sum of those values results in the current elements value. The element at index 0 is 0 The element at index 1 is equal to the element at index 0 + the element at index 1. So it is equal to 1 (0+1) The element at index 2 is equal to the sum of elements at index 0 and 1 + the element at index 2. So it is equal to 3 ((0+1)+2) And so on .... https://docs.python.org/3/library/itertools.html https://docs.python.org/3/library/itertools.html#itertools.accumulate

+5

Hacker-KR4636 Lothar JESUS EDUARDO CANUL KOYOC Please answerπŸ™πŸ»πŸ™πŸ»πŸ™πŸ»

+5

ChaoticDawg JaScript Hacker-KR4636 Thanks very much

+4

Caleb, that's very easy, you are using function accumulate, which takes an iterable as argument, that function sums all the numbers given in its argument. In your program, you are passing to accumulate an iterable range(8) --> 0,1,2,3,4,5,6,7 So: 0+1 = |1|+2 = |3|+3 = |6|+4 = |10|+5 = |15|+6 = |21|+7 = |28| You're also using list() which converts its argument in a list, So the output is: [1, 3, 6, 10, 15, 21, 28]

+3

The function results are x0, x0+x1, x0+x1+x2, …