简体   繁体   English

查找数组中最低/最长序列的初始位置

[英]Find the initial position of the lowest/longest sequence in an array

Similar to sleep cycles alarms, I need to cut my array in the best place possible (low numbers in my scenario) respecting a range of min/max amount of values... 与睡眠周期警报类似,我需要在最小/最大值范围内,将数组尽可能地切开(在我的场景中为低数字)。

To simplify, if I am able to find the longest lowest sequence in an array, I think I can move forward. 为简化起见,如果我能够找到数组中最长的最低序列,我想我可以继续前进。

For example: 例如:

[1,2,3,0,1,4,5,6,6,0.1,1.1,2,4]

Should return 9, because 0.1 is the first value of the longest lowest, even though I have a lower value than 0.1; 应该返回9,因为0.1是最长的最低值的第一个值,即使我的值小于0.1;

[4,5,7,10,0.13,0.2,0.12,8,9,28,0.1,0.11,0.102]

Should return 10, because it is lower than 1, even though, the sequence has the same amount of numbers... 即使该序列具有相同数量的数字,也应返回10,因为它小于1。

Longer sequences (in my scenario) are more important than lower. 较长的序列(在我的场景中)比较低的序列更重要。 Any idea how to start this? 知道如何开始吗? I don't have a threshold, but a solution involving this should be ok I think (if calculated on-the-fly) 我没有阈值,但是我认为可以解决此问题(如果即时计算)

I'm not sure about your convoluted logic behind "longest lowest", but this could be a good start: 我不确定您在“最长的最低价”之后是否费解的逻辑,但这可能是一个不错的开始:

data = [4,5,7,10,0.13,0.2,0.12,8,9,28,0.1,0.11,0.102]

result = [[0,0]]
threshold = 1.0
for num, val in enumerate(data) :
    if val < threshold :
        if result[-1][1] == 0 :   # start a new sequence
            result[-1][0] = num
            result[-1][1] = 1
        else :                    # continue existing sequence
            result[-1][1] += 1
    else :    # end the previous sequence
        if result[-1][1] > 0 :
            result.append([0,0])

returns (first element, sequence length) pairs: 返回(第一个元素,序列长度)对:

[[4, 3], [10, 3]]

that you may further analyse for the length, value of the first element or whatever you like. 您可以进一步分析第一个元素的长度,值或您喜欢的任何元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM