在python中使用not equal和nan [英] Using not equal and nan together in python
本文介绍了在python中使用not equal和nan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果Group [NN1,8]!='nan':
print(Group [NN1,8],集团[NN1,8])
集团的价值[NN1, 8]是nan,因此我希望打印命令不会执行。但是我的代码会执行。
结果是
`('Group [NN1,8]',nan)`。
无需打印。有什么问题吗?
解决方案
看起来你正在比较一个浮点数和一个永远不相等的字符串。
>>> float('nan')
nan
>>> 'nan'
'nan'
>>> float('nan')=='nan'
False
在特殊情况下 nan
,它甚至不等于本身:
> ;>> x = float('nan')
>>> x == x
错误
您可以改用:
>>>导入数学
>>> math.isnan(x)
True
I have strange problem. I have the following code
if Group[NN1,8] != 'nan' :
print("Group[NN1,8]",Group[NN1,8])
The value of Group[NN1,8] is nan,therefor i expect that print command not execute. But with my code it executes. Result is
`('Group[NN1,8]', nan)`.
which doesn't have to print. Is there any thing wrong?
解决方案
Looks like you're comparing a float with a string, which are never equal.
>>> float('nan')
nan
>>> 'nan'
'nan'
>>> float('nan') == 'nan'
False
In the special cases of nan
, it doesn't even equal "itself":
>>> x = float('nan')
>>> x == x
False
You can use this instead:
>>> import math
>>> math.isnan(x)
True
这篇关于在python中使用not equal和nan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文