Reputation: 35
I have a list and inside of the list, the values and their signs are changing intermittently. I would like to define a function to extract from the list in every two consecutive sign-changing (Let's say zero-crossing
points).
n = 5 #a number which comes from another list and here is written to make this example
List_1 = [-1.,-0.89, -0.77,-0.667, -0.55, -0.44,-0.333, -0.22, -0.111,0.,1,1.333,1.66,2,2.3,2.66,3.3, 3.667, 4,-30,-26.77,-23,-20.33-17.11,-13.8,-10,-7.44,-4.22, -1.,30,37.5,45,52.5,60]
def list_excluder:
for i in range(n):
zero_crossings = np.where(np.diff(np.sign(list_1[i])))[0]
GG = list_1[i][zero_crossings[i-1]:zero_crossings[i]]
ls.append(GG)
expected result:
ls = [[-1.,-0.89, -0.77,-0.667, -0.55, -0.44,-0.333, -0.22, -0.111,0.,1,1.333,1.66,2,2.3,2.66,3.3, 3.667, 4],[-30,-26.77,-23,-20.33-17.11,-13.8,-10,-7.44,-4.22, -1.,30,37.5,45,52.5,60.]]
Upvotes: 1
Views: 63
Reputation: 395
I guess, here is your answer based on the explanation which was given by you:
def finding_bends(data, arr):
zero_crossings = np.where(np.diff(np.sign(arr)))[0]
zero_crossings = np.array(zero_crossings)
BENDs = []
for i in range(len(zero_crossings)-1):
# print(i)
BENDs.append(data[zero_crossings[i]:zero_crossings[i+1]])
return BENDs
Upvotes: 1
Reputation: 3358
I think I understand just from looking at your input output sample.
List_1 = [-1.,-0.89, -0.77,-0.667, -0.55, -0.44,-0.333, -0.22, -0.111,0.,1,1.333,1.66,2,2.3,2.66,3.3, 3.667, 4,-30,-26.77,-23,-20.33,-17.11,-13.8,-10,-7.44,-4.22, -1.,30,37.5,45,52.5,60]
a = np.array(List_1)
i = np.flatnonzero((a[:-1] - a[1:]) > 0)
np.split(a, i+1)
Output:
[array([-1. , -0.89 , -0.77 , -0.667, -0.55 , -0.44 , -0.333, -0.22 ,
-0.111, 0. , 1. , 1.333, 1.66 , 2. , 2.3 , 2.66 ,
3.3 , 3.667, 4. ]),
array([-30. , -26.77, -23. , -20.33, -17.11, -13.8 , -10. , -7.44,
-4.22, -1. , 30. , 37.5 , 45. , 52.5 , 60. ])]
Upvotes: 2