时间:2022-11-04 09:52:24 | 栏目:Python代码 | 点击:次
该算法实现对列表中大于某个阈值(比如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)
效果如下: