Skip to content Skip to sidebar Skip to footer

Split An Array Dependent On The Array Values In Python

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],

Solution 1:

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 = 10array = [[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

Solution 2:

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 inrange(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

Solution 3:

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 valuesdif = ar[1:, 0] - ar[:-1, 0]

# get the index where you first observe a jumpfi = 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])

Solution 4:

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 indexif abs(cur[0] - nxt[0]) > 10:  # compare difference of first items
        arr1, arr2 = array[:idx+1], array[idx+1:]  # splitbreak# no further splits, end the loop nowelse:  # 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.

Solution 5:

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 =10for index, valuein enumerate(array[:-1]): # [:-1] prevents outofrange
    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 tooneof the arrays.

Post a Comment for "Split An Array Dependent On The Array Values In Python"