So for example say I have a list of times (0,1,2,3,4,5) in seconds, and I have another list of speeds at those times (0,0.5,1,1.5,2,2.5) in m/s.
How would I write a program that can tell me which speeds in the second list cross a threshold of 1 m/s and also the times at which those speeds were measured.
the output would look something like :
speeds that cross threshold: [1.5,2,2.5]
times that speed cross threshold: [3,4,5]
so far I've managed to create the first part using this code:
https://i.stack.imgur.com/ii8tF.png
I'm not sure how to get the specific times based on when those speeds are greater than the threshold.
CodePudding user response:
You could pair each time of measure with it's speed and then return a list with only the time values such that their speed is greater than the threshold.
time_list = [0,1,2,3,4,5]
speed_list = [0,0.5,1,1.5,2,2.5]
def find_times(times, speeds, threshold):
timexspeed = list(zip(times, speeds))
return [time for time, speed in timexspeed if speed > threshold]
find_times(time_list, speed_list, 1)
>>>[3, 4, 5]
Alternative, as I suggested in comments, you can use a strategy based on iterating through the length of the list instead of it's elements:
for i in range(len(list1)):
if list1[i] <some_condition>:
<do_domething_with> list2[i]
CodePudding user response:
You could use pandas
import pandas as pd
df = pd.DataFrame()
df['time'] = (0,1,2,3,4,5)
df['speed'] = (0,0.5,1,1.5,2,2.5)
cutoff = 1
df[df.speed >= cutoff].time