我有一個串列,在串列內部,值及其符號間歇性地變化。我想定義一個函式,以在每兩個連續的符號變化(假設zero-crossing點)中從串列中提取。
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)
預期結果:
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.]]
uj5u.com熱心網友回復:
我想我只是通過查看您的輸入輸出樣本來理解。
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)
輸出:
[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. ])]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/433826.html
