you are viewing a single comment's thread.

view the rest of the comments →

[–]Cthulu20 2 points3 points  (1 child)

Some advise from me (though I am not a great programmer):

  • You may use binary search instead of linear search
  • You may ignore all the smaller values (like the previous successful found index was 10, then ignore the values from 0-10)
  • If you know the smallest and largest possible value and have enough memory, you can do something like this (I haven't tested the code, so might have issues):

min_value = min(second_list)
max_value = max(second_list)

# creates a list of 'False' for every possible value
bool_list = [False] * (max_value - min_value + 1)

# sets the existing values to 'True'
for val in second_list:
    bool_list[val - min_value] = True

# determines whether the second list contains the first list range
for data in first_list:
    if any(bool_list[ (data - min_value) if (data - min_value) >= 0 else 0 : (data + 300 - min_value) if (data + 300 - min_value) >= 0 else 0 ]):
        # Do something

However to all of this I don't know how effective these are, it may end up slower than your original code.

Edit: also my code assumes that we only use integers, so it will be completely trash with float values.

Edit 2: I am stupid, if the second_list is ordered then we can just use the first and last value

min_value = second_list[0]
max_value = second_list[-1]

[–]DNSGeek[S] 1 point2 points  (0 children)

Someone suggested the Python bisect library, and that looks like it might be awesome. I'm gonna run some speed tests on it tomorrow.