zoukankan      html  css  js  c++  java
  • python处理孤立的异常点

    假设有一个列表,a = [61, 40, 70, 80, 86, 50, 88, 33, 76, 64],保存的是设备的状态值随时间的变化,超过60即为异常,但是对于孤立的异常点,我们需要将其忽略,只有连续的异常点才认为是真正的异常,需要统计异常的次数(当然也可以有其他的操作,删除孤立的异常点等等)。

    处理的代码如下:

    def get_above_threshold_num(device_status, threshold):
        if not isinstance(device_status, list) or not isinstance(threshold, int):
            return
        num = 0
        count = len(device_status)
        for index, value in enumerate(device_status):
            if value >= threshold:
                if (0 <= index - 1 < count and device_status[index - 1] >= threshold) or (0 <= index + 1 < count and device_status[index + 1] >= threshold):
                    num += 1
        return num
    
    
    if __name__ == "__main__":
        a = [61, 40, 70, 80, 86, 50, 88, 33, 76, 64]
        b = get_above_threshold_num(a, 80)
        print(b)

    思路即就是遍历列表中的元素,如果大于等于阈值,则找到它的前一个元素和后一个元素,只要有一个元素是大于等于阈值的,则进行统计。

  • 相关阅读:
    Flask基础01
    Django logging配置
    JSONP和CORS跨域
    Scrapy框架
    请求库之urllib,requests及工具selenium
    MongoDB安装
    Django 视图层
    Django REST framework 2
    WebSocket
    爬虫性能相关
  • 原文地址:https://www.cnblogs.com/lucky-heng/p/11191861.html
Copyright © 2011-2022 走看看