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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值