python 逗号分隔符 拆分成数组,根据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],[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?

解决方案

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

你可能感兴趣的:(python,逗号分隔符,拆分成数组)