Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

list comprehension division

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading