Zanam
Zanam

Reputation: 4807

Finding index of largest negative and smallest positive element in array

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.

Upvotes: 2

Views: 3643

Answers (1)

niraj
niraj

Reputation: 18208

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]))

Upvotes: 4

Related Questions