0

I have an array of coordinates, and I would like to split the array into two arrays dependent on the Y value when there is a large gap in the Y value. This post: Split an array dependent on the array values in Python does it dependent on the x value, and the method I use is like this:

array = [[1,5],[3,5],[6,7],[8,7],[25,25],[26,50],.....]
n = len(array)
for i in range(n-1): 
    if abs(array[i][0] - array[i+1][0]) >= 10:
       arr1 = array[:i+1]
       arr2 = array[i+1:]

I figured that when I want to split it dependent on the Y value I could just change:

if abs(array[i][0] - array[i+1][0]) to if abs(array[0][i] - array[0][i+1])

This does not work and I get IndexError: list index out of range.

I'm quite new to coding and I'm wondering why this does not work for finding gap in Y value when it works for finding the gap in the X value?

Also, how should I go about splitting the array depending on the Y value?

Any help is much appreciated!

1
  • 1
    abs(array[i][1] - array[i+1][1]) <<--- mind the indexing Commented Apr 5, 2017 at 12:03

1 Answer 1

1

you have to switch to this:

array = [[1,5],[3,5],[6,7],[8,7],[25,25],[26,50]]
n = len(array)
for i in range(n-1): 
    if abs(array[i][1] - array[i+1][1]) >= 10:

       arr1 = array[:i+1]
       arr2 = array[i+1:]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.