查找数组中最大负元素和最小正元素的索引 [英] Finding index of largest negative and smallest positive element in array

查看:45
本文介绍了查找数组中最大负元素和最小正元素的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组如下:

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

如何求最大负数和最小正数的index?

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

即在上面的示例中索引为 -2 和 2.

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

推荐答案

你可以试试,最多为负数:

You can try, for max of negative:

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

在上面,Arr[Arr<0] 将获得所有小于 0 或负数的数字,并将 max 应用于列表将给出最大负数.然后,它可以与 index 一起使用以获取列表中数字的索引.

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.

对于正数的最小值:

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

这篇关于查找数组中最大负元素和最小正元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆