[Numpy-discussion] indexing with booleans without making a copy?

Anne Archibald aarchiba at physics.mcgill.ca
Wed Sep 8 15:35:18 EDT 2010


2010/9/8 Ernest Adrogué <eadrogue at gmx.net>:
> I have a sorted, flat array:
>
> In [139]: a =np.array([0,1,2,2,2,3])
>
> Basically, I want views of the areas where there
> are repeated numbers (since the array is sorted, they
> will be contiguous).
>
> But, of course, to find the numbers that are repeated
> I have to use comparison operations that return
> boolean arrays, so I suppose the problem is converting
> the boolean array into a slice.

Well, you're going to have to do some allocation, but how's this? Use
unique1d to get an array of unique values. Then use searchsorted
twice, once to find the leftmost end of each hunk, and once to find
the rightmost end of each hunk.

Anne



More information about the NumPy-Discussion mailing list