Python code for counting number of zero crossings in an array

前端 未结 6 1460
春和景丽
春和景丽 2021-02-08 01:44

I am looking to count the number of times the values in an array change in polarity (EDIT: Number of times the values in an array cross zero).

Suppose I have an array:<

6条回答
  •  情深已故
    2021-02-08 02:32

    You can achieve it using list comprehension:

    myList = [80.6, 120.8, -115.6, -76.1, 131.3, 105.1, 138.4, -81.3, -95.3,  89.2, -154.1, 121.4, -85.1, 96.8, 68.2]
    len([x for i, x in enumerate(myList) if i > 0 and ((myList[i-1] > 0 and myList[i] < 0) or (myList[i-1] < 0 and myList[i] > 0))])
    

提交回复
热议问题