Finding index of largest negative and smallest pos

2019-08-25 17:13发布

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条回答
太酷不给撩
2楼-- · 2019-08-25 17:32

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]))
查看更多
登录 后发表回答