从numpy数组中删除彼此靠近的值 [英] Remove values from numpy array closer to each other

查看:69
本文介绍了从numpy数组中删除彼此靠近的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我想从numpy数组中删除彼此更靠近的元素.例如,我有数组[1,2,10,11,18,19],那么我需要可以给出类似[1,10,18]的输出的代码,因为2更接近于1,所以上.

Actually i want to remove the elements from numpy array which are closer to each other.For example i have array [1,2,10,11,18,19] then I need code that can give output like [1,10,18] because 2 is closer to 1 and so on.

推荐答案

这是在一维numpy数组中查找一系列连续值的第一个值的简单函数.

Here is simple function to find the first values of series of consecutives values in a 1D numpy array.

import numpy as np

def find_consec(a, step=1):
    vals = []
    for i, x in enumerate(a):
        if i == 0:
            diff = a[i + 1] - x
            if diff == step:
                vals.append(x)
        elif i < a.size-1:
            diff = a[i + 1] - x
            if diff > step:
                vals.append(a[i + 1])
    return np.array(vals)

a = np.array([1,2,10,11,18,19])
find_consec(a) # [1, 10, 18]

这篇关于从numpy数组中删除彼此靠近的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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