An example:
l = [1, 3, 8, 10, 20]
Result:
[2, 5, 2, 10]
I want to get the difference between the two numbers
Some efforts I tried:
- list-comprehension, I like this but if I want to do some nested list-comprehension, it is maybe a little hard to read:
print([l[i]-l[i-1] for i in range(1, len(l))])
- I tried to find it in itertools.I find some very similar function like
accumulate
(Evenfunctools.reduce
).Exactly, they couldn't get expected result.
Does there has some function in the standard library could achieve that?like:
func(lambda x,y: x-y, l)
If this question is duplicate, please tell me and I will delete this post.Thanks <3.