Reputation: 365
I have a list of time intervals in minutes that looks like the following:
lst = [9.7, 16.1, 16.5, 2.3, 0.2, 7.0, 4.8, 24.9, 17.6, 13.8, 12.3,
16.2, 2.4, 8.7, 2.7, 35.5, 0.9, 25.6, 1.2, 17.4, 6.2, 7.8, 27.7, 0.3,
0.9, 37.0, 10.7, 1.4, 65.3, 44.0, 1.8, 8.3, 2.2, 5.6, 14.6, 2.7, 17.9,
0.8, 2.8, 25.4, 7.7, 8.1, 13.6, 9.7, 7.3, 0.5, 19.4, 51.4, 56.1, 16.3]
I am trying to calculate the longest and shortest waiting time between the intervals in the list lst. I tried the following:
size = len(lst)
interval_diff = [lst[i + 1] - lst[i] for i in range(size) if i+1 < size]
print("shortest waiting time:",(min(interval_diff)))
print("longest waiting time:",(max(interval_diff)))
this gives me the result:
shortest waiting time: -42.2
longest waiting time: 63.9
the problem is that I am being able to calculate the longest waiting time correctly but the shortest waiting time is giving me -42.2 and a time cannot be negative. If I try using absolute value it would just give me positive 42.2 and that is not the shortest waiting time, if we compare each time interval on the list with the next one. I cannot sort the list either because it needs to be in the order in which the intervals are now. Does anyone have any idea of how could I obtain the shortest waiting time of the list? Which in this case, I guess it should be something like 2.3-0.2 which is 2.1? Any help would be greatly appreciated.
Upvotes: 0
Views: 1090
Reputation: 5425
Instead of doing min
and then abs
, take the absolute difference between each time:
interval_dff = [abs(x - y) for x, y in zip(lst[1:], lst)]
Then you can just use min
/max
Upvotes: 2