+ 2

Example

why is that works so: nums = list(accumulate(range(8))) ?

2nd Oct 2016, 12:10 PM
Николай Чернов
Николай Чернов - avatar
1 Resposta
+ 6
It helps to do things in the python shell, one at a time. Extra steps are here for hints: >>> from itertools import accumulate >>> list(accumulate(range(8))) [0, 1, 3, 6, 10, 15, 21, 28] >>> print(range(8)) range(0, 8) >>> list(range(8)) [0, 1, 2, 3, 4, 5, 6, 7] >>> accumulate(range(8)) <itertools.accumulate object at 0xb6af7ff0> >>> list(accumulate(range(8))) [0, 1, 3, 6, 10, 15, 21, 28] loop accumulator 0 0 1 1 (0+1) 2 1+2=3 (new value) 3 3+3=6 4 6+4=10 5 10+5=15 etc. Another example, accumulating 1 four times, then 2 on the last iteration: >>> list(accumulate([1,1,1,1,2])) [1, 2, 3, 4, 6]
2nd Oct 2016, 2:33 PM
Kirk Schafer
Kirk Schafer - avatar