Large list, find all minima of list (python)

前端 未结 3 1725
无人共我
无人共我 2021-01-07 17:02

Given a large list of fluctuating values, how do you determine all local min values? Not using numpy. Local minimum means all values in a list that are the troughs

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-07 18:03

    def local_min(ys):
        return [y for i, y in enumerate(ys)
                if ((i == 0) or (ys[i - 1] >= y))
                and ((i == len(ys) - 1) or (y < ys[i+1]))]
    
    
    >>> local_min([23, 8, -7, 57, 87, 6])
    [-7, 6]
    >>> local_min([23, 6, 6, 6, 42])
    [6]
    >>> local_min([6, 6, 4])
    [4]
    

提交回复
热议问题