Split array at value in numpy

后端 未结 4 1724
逝去的感伤
逝去的感伤 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:17

    You don't need a python loop to evaluate the locations of each split. Do a difference on the first column and find where the values decrease.

    import numpy
    
    # read the array
    arry = numpy.fromfile(file, dtype=('float, S2'))
    
    # determine where the data "splits" shoule be
    col1 = arry['f0']
    diff = col1 - numpy.roll(col1,1)
    idxs = numpy.where(diff<0)[0]
    
    # only loop thru the "splits"
    strts = idxs
    stops = list(idxs[1:])+[None]
    groups = [data[strt:stop] for strt,stop in zip(strts,stops)]
    

提交回复
热议问题