Finding index of largest negative and smallest pos

2019-08-25 17:28发布

问题:

I have an array as follows:

import numpy as np    
Arr = np.array([-10, -8, -8, -6, -2, 2, 4, 19])

How do I find the index of largest negative and smallest positive number?

i.e in the above example index of -2 and 2.

回答1:

You can try, for max of negative:

list(Arr).index(max(Arr[Arr<0]))

In above, Arr[Arr<0] will get all numbers less than 0 or negative and applying max to the list will give max of negative. Then, it can be used with index to get the index of number in list.

And for min of positive:

list(Arr).index(min(Arr[Arr>0]))