I have two lists in python and I want to know if they intersect at the same index. Is there a mathematical way of solving this?
For example if I have [9,8,7,6,5] and [3,4,5,6,7] I’d like a simple and efficient formula/algorithm that finds that at index 3 they intersect. I know I could do a search just wondering if there is a better way.
I know there is a formula to solve two lines in y = mx + b form by subtracting them from each other but my “line” isn’t truly a line because its limited to the items in the list and it may have curves.
Any help is appreciated.
You could take the set-theoretic intersection of the coordinates in both lists:
…enumerate gives you an iterable of tuples of indexes and values – in other words, (0,9),(1,8),(2,7),etc.
http://docs.python.org/library/stdtypes.html#set-types-set-frozenset
…make sense? Of course, that won’t truly give you geometric intersection – for example, [1,2] intersects with [2,1] at [x=0.5,y=1.5] – if that’s what you want, then you have to solve the linear equations at each interval.