python找出列表中大于某个阈值的数据段示例

Irisa ·
更新时间:2024-09-21
· 799 次阅读

该算法实现对列表中大于某个阈值(比如level=5)的连续数据段的提取,具体效果如下:

找出list里面大于5的连续数据段:

list = [1,2,3,4,2,3,4,5,6,7,4,6,7,8,5,6,7,3,2,4,4,4,5,3,6,7,8,9,8,6,1]

输出:

[[6, 7], [6, 7, 8], [6, 7], [6, 7, 8, 9, 8, 6]]

算法实现:

# -*- coding: utf-8 -*- """ -------------------------------------------------------- # @Version : python3.6 # @Author : wtg # @File : data_search.py # @Software: PyCharm # @Time : 2018/12/17 14:44 -------------------------------------------------------- # @Description: -------------------------------------------------------- """ def data_search(data, level): list = [] temp = [] for i in range(len(data)): if data[i] > level: temp.append(data[i]) else: list.append(temp) temp = [] return [i for i in list if i] if __name__ == '__main__': list = [1,2,3,4,2,3,4,5,6,7,4,6,7,8,5,6,7,3,2,4,4,4,5,3,6,7,8,9,8,6,1] ret = data_search(list, 5) print("input: ",list) print("output: ",ret)

效果如下:

以上这篇python找出列表中大于某个阈值的数据段示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。

您可能感兴趣的文章:python 筛选数据集中列中value长度大于20的数据集方法Python 找到列表中满足某些条件的元素方法Python进行数据提取的方法总结python 返回列表中某个值的索引方法



数据 阈值 Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号