Finding index of largest negative and smallest positive element in array

99封情书 提交于 2019-12-11 18:23:43

问题


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


来源:https://stackoverflow.com/questions/50224895/finding-index-of-largest-negative-and-smallest-positive-element-in-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!