在python中的numpy数组(或元组)中找到不包括零的最小值/最大值 [英] Find the min/max excluding zeros in a numpy array (or a tuple) in python

查看:1541
本文介绍了在python中的numpy数组(或元组)中找到不包括零的最小值/最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组.有效值不为零(正数或负数).我想找到不应该考虑零的数组中的最小值和最大值.例如,如果数字仅是负数.零将是有问题的.

I have an array. The valid values are not zero (either positive or negetive). I want to find the minimum and maximum within the array which should not take zeros into account. For example if the numbers are only negative. Zeros will be problematic.

推荐答案

怎么样:

import numpy as np
minval = np.min(a[np.nonzero(a)])
maxval = np.max(a[np.nonzero(a)])

其中a是您的数组.

这篇关于在python中的numpy数组(或元组)中找到不包括零的最小值/最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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