有没有办法以更简洁(或更多Python)的方式编写以下代码?
[ counters[i]-counters[i-1] for i in range(1, len(counters)) ]
答案 0 :(得分:1)
您可以使用zip
:
[b-a for a,b in zip(counters,counters[1:])]
counters = [1,6,3,8,4,6]
print [b-a for a,b in zip(counters,counters[1:])]
[OUTPUT]
[5, -3, 5, -4, 2]