It's ok to use range
. However, programming (like maths) is about building on abstractions. Consecutive pairs [(x0, x1), (x1, x2), ..., (xn-2, xn-1)], are called pairwise combinations. See an example in the itertools docs. Once you have this function in your toolset, you can write:
for x, y in pairwise(xs):
print(y - x)
Or used as a generator expression:
consecutive_diffs = (y - x for (x, y) in pairwise(xs))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…