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)
效果如下:
以上这篇python找出列表中大于某个阈值的数据段示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。