零分割错误,浮点除零

时间:2017-11-07 07:36:01

标签: python

这是我的代码

for i in range(len(speed)):
for j in range(len(time_two)):
    new.append((speed[i] - speed[i-1])/(time_two[j] - time_two[j-1]))

我收到此错误:

ZeroDivisionError                         Traceback (most recent call  
last)
    <ipython-input-7-9cdf386300d7> in <module>()
      4 for i in range(len(speed)):
      5     for j in range(len(time_two)):
    ----> 6         new.append((speed[i] - speed[i-1])/(time_two[j] - 
    time_two[j-1]))

ZeroDivisionError: float division by zero

这里speed和time_two是浮动列表, 列表中没有0。

关于我可能会改变什么的任何建议?任何帮助表示赞赏!提前谢谢

编辑: 速度= [14.13102608620676,  6.111463527087486,  5.593147106275493,  4.854037993898749,...] time_two = [2.0,  14.0,  15.0,  17.0,  15.0,  15.0 ...]

1 个答案:

答案 0 :(得分:3)

time_two = [2.0, 14.0, 15.0, 17.0, 15.0, 15.0...]

有两个相同的值,所以如果你这样做

time_two[j] - time_two[j-1]

你得零,所以

(speed[i] - speed[i-1])/(time_two[j] - time_two[j-1])

会触发ZeroDivisionError