0

How can you split a numpy array of any datatype wherever the sequence changes. Examples of Inputs and desired outputs below:

# Inputs
a = np.array([1,2,3,3,3,4,4,5,6,7,7,8,8,9])
b = np.array(["a","b","c","c","c","d","d","e","f","g","g","h","h","i"])
# Desired outputs
a:
[[1], [2], [3, 3, 3], [4, 4], [5], [6], [7, 7], [8, 8], [9]]
b:
[['a'],['b'],['c', 'c', 'c', 'c'],['d', 'd'],['e'],['f'],['g', 'g'],['h', 'h'],['i']]

2 Answers 2

3

Since your output consists of lists that are not equal sized, it can't be fully functional numpy array. Then you need to use Python tricks:

from itertools import groupby
[list(g) for k, g in groupby(a)]
[list(g) for k, g in groupby(b)]

If speed matters as well, use a=a.tolist() and b=b.tolist() before grouping.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is an example way how todo it:

def consecutive_split(data: np.ndarray):
    b = np.roll(data,1)    
    return np.split(data,np.where((data!=b)[1:])[0]+1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.