np.isnan() == False,但 np.isnan() 不是 False

np.isnan() == False, but np.isnan() is not False(np.isnan() == False,但 np.isnan() 不是 False)

本文介绍了np.isnan() == False,但 np.isnan() 不是 False的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,== 检查值是否相等,is 检查值后面的结构的身份(例如 === 其他语言).

As far as I understand it, == checks for equality of value, and is checks for identity of structure behind value (as, say === in some other languages).

鉴于此,我不明白以下内容:

Given that, I don't understand the following:

np.isnan(30) == False
Out[19]: 
True
np.isnan(30) is False
Out[20]: 
False

其他身份检查似乎并非如此:

It appears not to be the case with other identity checks:

(5 == 4) == False
Out[22]: 
True
(5 == 4) is False
Out[23]: 
True

看起来好像 np.isnan() 返回 False 作为值,而不是作为标识.为什么会这样?

It appears as if np.isnan() returns False as a value, but not as identity. Why is that the case?

推荐答案

numpy.isnan() 返回一个compatible 类型的对象:

numpy.isnan() returns a compatible type object:

>>> import numpy
>>> type(numpy.isnan(0))
<class 'numpy.bool_'>

这是一个自定义布尔值,可以有效地存储在 numpy 数组中,请参阅 Numpy 的 数据类型 文档.numpy.isnan() 函数也可以对数组进行操作,生成另一个数组并得到结果:

This is a custom boolean that can be stored efficiently in numpy arrays, see Numpy's Data Types documentation. The numpy.isnan() function can also operate on arrays, producing another array with results:

>>> numpy.isnan(numpy.array([1, 2]))
array([False, False], dtype=bool)

dtype 又是 Numpy 布尔对象.

where again the dtype is the Numpy boolean object.

Python 不保证布尔运算必须始终返回单例布尔值.你永远不应该测试 is Trueis False anyway.在布尔运算中直接使用 numpy.isnan() 输出,使用 not 来测试假值:

Python makes no guarantees that boolean operations must always return a singleton boolean value. You should never test for is True or is False anyway. Use numpy.isnan() output directly in boolean operations, use not to test for false values:

if numpy.isnan(foo):

if not numpy.isnan(bar):

这篇关于np.isnan() == False,但 np.isnan() 不是 False的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:np.isnan() == False,但 np.isnan() 不是 False

基础教程推荐