1

I have a NumPy array of values, and I am needing to identify groups of consecutive values within the array.

I've tried writing a "for" loop to do this, but I'm running into a host of issues. So I've looked at the documentation for groupby in itertools. I've never used this before, and I'm more than a little confused by the documentation, so here I am.

Could someone give a more "layman speak" explanation of how to use groupby? I don't need a sample code, per se, just a more thorough explanation of the documentation.

1

1 Answer 1

1

a good answer to this is to use a generator to group it (might not be the fastest method)

def groupings(a):
     g = []
     for val in a:
         if not g:
             g.append(val)
         elif abs(g[-1] - val) <= 1.00001:
             g.append(val)
         else:
             yield g
             g = []

print list(groupings(my_numpy_array))

I know this doesnt give you a laymans explanation of group by (group consecutive items that match some criteria... it would be somewhat painful for this type of application)

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

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.