[Numpy-discussion] Run length encoding of an ndarray

Michael Hoffman b3i4old02 at sneakemail.com
Tue Oct 2 09:13:48 EDT 2007


Michael Hoffman wrote:
> I am trying to do a type of run-length encoding of a 2D array by axis. I 
> have an array of values arranged along two axes, state and position. 
> These are many (180, 30000) uint8 arrays.
> 
> I would like to have a list of tuples like
> 
> (state, start_pos, end_pos, values)
> 
> only separating out a set of values into a new tuple if they are all the 
> same value in a run of at least 10 cells.
> 
> Is there a clever way to do this in NumPy? I was thinking of using 
> itertools.groupby() but it would be nicer to have something faster.

I just realized I could convert this to a string and use something like 
re.compile(r"(.)\1{9,}").finditer() but I'm still wondering if there's 
something more NumPythonic.




More information about the NumPy-Discussion mailing list