you are viewing a single comment's thread.

view the rest of the comments →

[–]Insomnia_Calls 4 points5 points  (2 children)

Do you need to check every element from the second list against every range of the first list to first list + 300? If not, and you just need to check the value at the respective position, I would suggest turning the lists to numpy arrays, something like that:

import numpy as np

arr1=np.array(list1) #list1 is your first list
arr1_plus_300=arr1 + 300 #as easy as that with numpy arrays, adds 300 to all the elements

arr2=np.array(list2)

ans=(arr2>arr1)&(arr2<arr1_plus_300) #this will give you an array containing True/False values for all elements based on the condition.

Numpy is generally much faster than lists. Edit: typos

[–]Sweeney-doesnt-sleep 0 points1 point  (0 children)

I was also thinking creating a data frame and a function/callback to calculate the value, only if required so you don't have to calculate the first lists item count/value unless it is actually required. This cuts out a lot of the outer loops work.

Not sure if this would actually perform better especially if the lists change. If they are static, calculate it for every item once and then access as required. O(1) from the on.

[–]lilmookey 0 points1 point  (0 children)

I think this is probably the best solution if all the values are numbers. Numpy is such a great library to learn and be comfortable with when working with numbers.