小编use*_*189的帖子

在python中查找数组的转折点

例如,如果我有一个数组:

A = (0,2,3,4,5,2,1,2,3,4,5,6,7,8,7,6,5,4,5,6)
Run Code Online (Sandbox Code Playgroud)

可以看出有4个转折点。(在 A[4]、A[6]、A[13]、A[17])

如何使用python返回转折点的数量?

import numpy as np
import scipy.integrate as SP
import math

def turningpoints(A):
    print A
    N = 0
    delta = 0
    delta_prev = 0
    for i in range(1,19):
        delta = A[i-1]-A[i]       #Change between elements
        if delta < delta_prev:    #if change has gotten smaller
            N = N+1               #number of turning points increases
        delta_prev = delta        #set the change as the previous change
    return N

if __name__ == "__main__":
    A  = np.array([0,2,3,4,5,2,1,2,3,4,5,6,7,8,7,6,5,4,5,6])
    print turningpoints(A)
Run Code Online (Sandbox Code Playgroud)

目前,这个系统是有缺陷的,当然不是很优雅。有任何想法吗?

python arrays python-2.7 minima

4
推荐指数
2
解决办法
1万
查看次数

标签 统计

arrays ×1

minima ×1

python ×1

python-2.7 ×1