查找数组中最大负元素和最小正元素的索引

编程入门 行业动态 更新时间:2024-10-15 12:32:47
本文介绍了查找数组中最大负元素和最小正元素的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个数组如下:

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

更多推荐

查找数组中最大负元素和最小正元素的索引

本文发布于:2023-11-29 11:53:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   组中   最小   索引

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!