简体   繁体   中英

Dividing list elements and append them to a new list

I have a list V which represents Velocity:

V = [1, 6, 8, 19, 19.5, 20, 21]

and I want to get a new list called distance . The distance is the old distance + Velocity/3600 (To change from Km/h to Km/s). This is my code:

disntance = 0    
for k in range(len(V)):
    distance[k] = V[k]/3600 + distance[k-1]

and I'm getting an error message:

out of the index or I cant divide list to int

My expected output is:

distance = [0.000277, 0.00194, 0.0041667, 0.00944, 0.01486, 0.020416, 0.02625]

You've mixed up int and list here.

If you want to track distance with a 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]

Or with an int only:

v = [1,6,8,19,19.5,20,21]
distance = 0;

for k in range(len(v)):
    distance += v[k]/3600
    
distance
# 0.02625

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM