根据Python中的数组值拆分数组 [英] Split an array dependent on the array values in Python

查看:108
本文介绍了根据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]]

我想在 6 之间拆分数组。和 7 。坐标([[5,7],[18,6]),因为那里的 X 值存在缺口。我想得到两个单独的数组, arr1 arr2 ,其中 arr1 是分割之前的值, arr2 是分割后的值。

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.

我想说的是,如果下一个 X 值大于的差10 ,它将追加到 arr2 ,否则为 arr1 ,如下所示:

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?

推荐答案

您可以执行以下操作:

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

然后 dif 为:

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]

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中的数组值拆分数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆