Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis0
why does accumulate increments value in defined manner(adding the previous list values only with natural numbers)?
Is there alternate for it?
2 Respostas
0
What do you mean by why?
I am not sure if you know this already.
from itertools import accumulate, takewhile
nums = list(accumulate(range(8)))
print(nums)
_________________
[0, 1, 3, 6, 10, 15, 21, 28]
range function makes
[0,1,2,3,4,5,6,7]
and accumulate function does
[0,0+1,0+1+2,0+1+2+3,0+1+2+3+4,and so on] #it adds previous list items
this equals to [0,1,3,6,10,15, etc]
0
The other way to do this is to
use def function(i think you know this already)