Home > Mobile >  list comprehension division
list comprehension division

Time:09-16

hello I am trying to divide two lists of distance and time differences however sometimes the time difference is 0 I don't know how to account for this in list comprehension.

velocity = [j / i for i, j in zip(distance[1:], time_diff[:-1]) if time_diff[i] > 0]

I get this error for this implementation.

TypeError: list indices must be integers or slices, not float

CodePudding user response:

As the comment said, in your if condition, you should add i>0 because for i, j in zip(distance[1:], time_diff[:-1]) gives you the element in your lists, not indices of the list.

velocity = [j / i for i, j in zip(distance[1:], time_diff[:-1]) if i > 0]
  • Related