我有一个V
代表速度的列表:
V = [1, 6, 8, 19, 19.5, 20, 21]
,我想获得一个名为distance
的新列表。距离是旧距离+ Velocity/3600
(从Km / h更改为Km / s)。这是我的代码:
disntance = 0
for k in range(len(V)):
distance[k] = V[k]/3600 + distance[k-1]
我收到一条错误消息:
out of the index or I cant divide list to int
我的预期输出是:
distance = [0.000277, 0.00194, 0.0041667, 0.00944, 0.01486, 0.020416, 0.02625]
答案 0 :(得分:1)
您在这里混合了int
和list
。
如果要使用list
跟踪距离:
v = [1,6,8,19,19.5,20,21]
distance = [0];
for k in range(len(v)):
distance.append(v[k]/3600 + distance[k])
distance
#[0, 0.0002777777777777778, 0.0019444444444444446, 0.004166666666666667,
# 0.009444444444444445, 0.014861111111111111,
# 0.020416666666666666, 0.02625]
或仅使用int
:
v = [1,6,8,19,19.5,20,21]
distance = 0;
for k in range(len(v)):
distance += v[k]/3600
distance
# 0.02625