Remove consecutive duplicates in a NumPy array

后端 未结 2 2478
执笔经年
执笔经年 2020-12-06 14:40

I would like to remove duplicates which follow each other, but not duplicates along the whole array. Also, I want to keep the ordering unchanged.

So if the input is

2条回答
  •  心在旅途
    2020-12-06 14:58

    For pure python wich also works with numpy arrays use this:

    def modify(l):
        last = None
        for e in l:
            if e != last:
                yield e
    
            last = e
    
    pure = modify([0, 0, 1, 3, 2, 2, 3, 3])
    
    import numpy
    num = numpy.array(modify(numpy.array([0, 0, 1, 3, 2, 2, 3, 3])))
    

    I don't know if there are any numpy functions wich would speed this up.

提交回复
热议问题