I'm trying to iterate of a list of tuples while still having access to the previous tuple and the next tuple.Specifically I need to compare the y coordinate of the previous current and next tuple.
This is my list of tuples that I'm using as input:
[(1,1),(2,2),(3,4),(4,3),(5,5),(6,3.5),(7,7),(8,8),(9,9),(10,8),(11,9.5),(11,7.5),(12,12),(13,1.5)]
I initially used this code segment to be able to have access to the previous, current and next elements:
def previous_and_next(some_iterable):
prevs, current, nexts = tee(some_iterable, 3)
prevs = chain([None], prevs)
nexts = chain(islice(nexts, 1, None), [None])
return zip(prevs, current, nexts)
But I can't access the elements of the tuple using this function as it returns an error about subscripting. Im open to new ideas or different functions, as this bit is clearly not what I need.
For more clarification this is the function that I am currently trying to implement
UF = UnionFind()
sortedlist = sorted(pointcloud, key=lambda x: x[1])
for previous, current, nxt in previous_and_next(sortedlist):
if previous[1] > current[1] and nxt[1] > current[1]:
UF.insert_objects(current)
elif previous[1] < current[1] and nxt[1] < current[1]:
c=UF.find(previous[1])
d=UF.find(nxt[1])
UF.union(c,d)
else:
c=UF.find(previous[1])
UF.union(current,previous)
return