6

I have an array of coordinates like this:

array = [[1,6],[2,6],[3,8],[4,10],[5,6],[5,7],[18,6],[19,5],[17,9],[10,5]]

I want to split the array between 6. and 7. coordinate ([5,7],[18,6]) because there is a gap in the X value there. I want to get two separate arrays, arr1 and arr2, where arr1 is the values before the split and arr2 is the values after.

I want to say that if the next X value is larger than a difference of 10, it will append to arr2, else arr1, something like this:

arr1 = []
arr2 = []
for [x,y] in array: 
    if next(x) > 10:
        arr2.append(x,y)
    else:
        arr1.append(x,y)

Can someone please help me with this problem?

6
  • The > 10: is a hard-coded attempt then, the number 10 isn't relevant, only the split in continuity? What about [5,6],[5,7], this should be ignored too? Commented Mar 7, 2017 at 14:02
  • Also, your appends should look like arr1.append([x,y]) to be consistent with your original list Commented Mar 7, 2017 at 14:06
  • Actually it's not clear to me at all why this would only result in two arrays from the example input, the split seems arbitrary. Commented Mar 7, 2017 at 14:06
  • Do you just need to split based on index where first difference greater than 10? Commented Mar 7, 2017 at 14:09
  • The 10 is a hard-coded attempt, if there is a better way please let me know ;) The array size may change, but there will always be one lagre "jump" in the x value somewhere, and this is where I want to split the array. Commented Mar 7, 2017 at 14:14

7 Answers 7

3

If I got your question correctly, you're trying to split the array at the first point where the difference between two subsequent x-values is greater than 10. You can do that using numpy:

import numpy as np
THRESH = 10
array = [[1,6],[2,6],[3,8],[4,10],[5,6],[5,7],[18,6],[19,5],[17,9],[10,5]]
array = np.asarray(array)
deltas_x = np.abs(array[1:,0] - array[:-1,0])
split_idx = np.where(deltas_x > THRESH)[0][0] + 1
arr1 = array[:split_idx,:]
arr2 = array[split_idx:,:]

Note that we need to add 1 to the result of np.where to account for the fact that the deltas_x array is 1 value shorter than array

Sign up to request clarification or add additional context in comments.

Comments

3

This might be what you are looking for

array = [[1,6],[2,6],[3,8],[4,10],[5,6],[5,7],[18,6],[19,5],[17,9],[10,5]]
# Declare two array variables
arr1 = None
arr2 = None
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:]
       break

print arr1
print arr2

2 Comments

How would one do it with respect to the Y value? changing if abs(array[i][0] - array[i+1][0]) >= 10: to if abs(array[0][i] - array[0][i+1]) >= 10: just returns an IndexError: list index out of range
That should return out of range error since the range of for loop depends on the number of elements in array. If you want to deal with Y values, you can write an inner for loop.
2

You can do the following:

ar = np.array([[1,6],[2,6],[3,8],[4,10],[5,6],[5,7],[18,6],[19,5],[17,9],[10,5]])

# get differences of x values
dif = ar[1:, 0] - ar[:-1, 0]

# get the index where you first observe a jump
fi = np.where(abs(dif) > 10)[0][0]

ar1 = ar[:fi+1]
ar2 = ar[fi+1:]

Then dif would be:

array([ 1,  1,  1,  1,  0, 13,  1, -2, -7])

fi would be 5 and ar1 and ar2 would be:

array([[ 1,  6],
       [ 2,  6],
       [ 3,  8],
       [ 4, 10],
       [ 5,  6],
       [ 5,  7]])

and

array([[18,  6],
       [19,  5],
       [17,  9],
       [10,  5]]),

respectively.

That would also allow you to get all jumps in your data (you would just have to change fi = np.where(abs(dif) > 10)[0][0] to fi = np.where(abs(dif) > 10)[0])

Comments

2

Probably easiest to iterate over successive pairs and split as soon as a gap is found:

array = [[1,6],[2,6],[3,8],[4,10],[5,6],[5,7],[18,6],[19,5],[17,9],[10,5]]

for idx, (cur, nxt) in enumerate(zip(array, array[1:])):  # successive pairs and index
    if abs(cur[0] - nxt[0]) > 10:  # compare difference of first items
        arr1, arr2 = array[:idx+1], array[idx+1:]  # split
        break  # no further splits, end the loop now
else:  # no break, keep the original array
    arr1, arr2 = array, []

Which gives:

>>> arr1
[[1, 6], [2, 6], [3, 8], [4, 10], [5, 6], [5, 7]]
>>> arr2
[[18, 6], [19, 5], [17, 9], [10, 5]]

It would be a bit more difficult if you wanted to split several times but this should well in your case.

Comments

2

When comparing consecutive elements I usually use enumerate:

array = [[1,6],[2,6],[3,8],[4,10],[5,6],[5,7],[18,6],[19,5],[17,9],[10,5]]
arr1 = list()
arr2 = list()
gap = 10

for index, value in enumerate(array[:-1]): # [:-1] prevents out of range
    if abs(value[0]-array[index+1][0]) >= gap:
        arr1.append(value)
    else:
        arr2.append(value)

arr2.append(array[-1])  # Take into account that the last element needs to be added to one of the arrays.

Comments

1
arry1 = []
arry2 = []
for i in arry:
    if (i[0] - i[1]) > 10:
        arry1.append(i)
    else:
        arry2.append(i)

2 Comments

Use a comment, not answer
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
0

Try this:

prev = array[0][0] 
pos = -1 
for i in range (1, len(array)): 
  if array[i][0] - prev >1: 
    break  
  else:  
    prev = array[i][0]  
if pos != -1:  
  arr1 = array[:pos]  
  arr2 = array[pos:] 

This should split array the way you want. Note that lists are indexed from 0.

3 Comments

This is not an answer.
Thanks for the response! I should have mentioned that the size of the array may change, but there will always be a gap in the X value somewhere. That is why I want a method to split dependent of the X value.
This should find the first place in the array where the x values are not continous. Thanks! @Rohan Sadale

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.