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.

Palmetto_Girl86
  • 917
  • 2
  • 10
  • 19
  • See [this question](http://stackoverflow.com/questions/4651683/numpy-grouping-using-itertools-groupby-performance). – Ami Tavory Jun 03 '15 at 17:26

1 Answers1

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)

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179