Split array at value in numpy

后端 未结 4 1720
逝去的感伤
逝去的感伤 2020-12-10 14:10

I have a file containing data in the format:

0.0 x1
0.1 x2
0.2 x3
0.0 x4
0.1 x5
0.2 x6
0.3 x7
...

The data consists of multiple datasets, e

4条回答
  •  一整个雨季
    2020-12-10 14:26

    Once you have the data in a long numpy array, just do:

    import numpy as np
    
    A = np.array([[0.0, 1], [0.1, 2], [0.2, 3], [0.0, 4], [0.1, 5], [0.2, 6], [0.3, 7], [0.0, 8], [0.1, 9], [0.2, 10]])
    B = np.split(A, np.argwhere(A[:,0] == 0.0).flatten()[1:])
    

    which will give you B containing three arrays B[0], B[1] and B[2] (in this case; I added a third "section" to prove to myself that it was working correctly).

提交回复
热议问题