Python Tutorial

Sunday, September 28, 2014

Python: Absolute difference between successive elements in list


See also abs, zip and islice
from itertools import islice

a = [1, 4 , 2 , 6 , 7, 3]
result = [abs(p-q) for p, q in zip(a, islice(a, 1, None))]

print result

Output:
[3, 2, 4, 1, 4]

0 comments:

Post a Comment