I have 4 lists consisting of time data in UNIX and a corresponding velocity. One set is larger than the other. I want to find the average velocity of a few values before and after the found value of the larger list at every matching time stamp of the smaller list.
t1 = [2, 5, 7]
t2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
v1 = [0.5, 1, 0.7]
v2 = [0.1, 0.5, 1, 1.3, 1.4, 1.8, 0.9, 2, 1.5, 1.2]
So if t1 and t2 match up, I want to get the average velocity of x values before and after that time.
let say x = 1 In this case it should give me the average of (0.1, 0.5, 1) (1.3, 1.4, 1.8) and (1.8, 0.9, 2)
Try
import numpy as np
for num in t1: #for each value in t1
try:
match_index=t2.index(num) #check if match exists
except:
match_index=-1 #otherwise set it to -1
if(match_index!=-1): #if match was found
my_list=[] #make an empty list
if(match_index==0): #if index was 0, then nearby 3 elements can't be found, only 2 can
my_list=v2[:match_index+2]
elif(match_index==len(v2)-1): #similarly if index was for last element
my_list=v2[match_index-1:]
else:
my_list=v2[match_index-1:match_index+2] #normal case slice the required values
print("average of :",my_list," is ",np.average(my_list))
#average of : [0.1, 0.5, 1] is 0.5333333333333333
#average of : [1.3, 1.4, 1.8] is 1.5
#average of : [1.8, 0.9, 2] is 1.5666666666666667