可能重复:
Python: Looping through all but the last item of a list阅读其他项目,同时通过它迭代
是否有通过列表迭代时,你还需要下一个项目一个更好的方法(或任何其他任意项目)在列表中?我用这个,但也许有人可以做的更好......
values = [1, 3, 6, 7 ,9]
diffs = []
for i in range(len(values)):
try: diffs.append(values[i+1] - values[i])
except: pass
print diffs
给出:
[2, 3, 1, 2]
相关:http://stackoverflow.com/questions/323750/。 – 2010-10-31 09:47:00