I have a numpy array, provided at random, which for this example looks like:
a = [10, 8, 6, 4, 2, -2, -4, -6, -8, -10, 1]
ideally, in this example, the values would be between -10 and 10 but this cannot be guaranteed (as above).
I want to retrive the 2 values closest to zero, such that:
b = a[a > 0][-1]
c = a[a < 0][0]
which would ideally return me the values of 2 and -2. However, the 1 value is included in the slice in b and i get returned the values of 1 and -2.
Is there a way in numpy to retrieve the values immediately 'next' to zero?
Its worth noting that whilst I always want to split the array at 0, the array could be any length and I could have an uneven number of positive and negative values in the array (i.e. [5, 4, 3, 2, 1, 0, -1])
A real world example is:
I want the yellow and green position but get returned the blue and green position instead, as the data crosses back over zero from -ve to +ve

1be the closest value that is close to0in the first example; in the second example should you return0or1-1?;[2, -2],[-10, 1], or both? Can we assume that the numbers are monotonically decreasing?