Split an array depending on array values ​​in Python

I have such an array of coordinates:

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 an array between 6. and 7. coordinate ([5,7],[18,6]), because there is a space in the value X. I want to get two separate arrays: arr1and arr2, where arr1are the values ​​before the section, and arr2are the values ​​after.

I want to say that if the following value is Xgreater than the difference in 10, it will be added 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 anyone help me with this problem?

+4
source share
7 answers

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 it difwill be:

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

fi 5 ar1 ar2 :

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

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

.

( fi = np.where(abs(dif) > 10)[0][0] fi = np.where(abs(dif) > 10)[0])

+1

,

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

, , x 10. 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:,:]

, 1 np.where, , deltas_x 1 array

+2

, , :

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

:

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

, , .

+1

:

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.
+1
arry1 = []
arry2 = []
for i in arry:
    if (i[0] - i[1]) > 10:
        arry1.append(i)
    else:
        arry2.append(i)
0

:

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 the arrayway you want. Note that lists are indexed from 0.

-1
source

Source: https://habr.com/ru/post/1671710/


All Articles