Python和Numpy的nan和set [英] Python's and Numpy's nan and set

查看:424
本文介绍了Python和Numpy的nan和set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Python的Numpy,set和NaN(非数字)的意外行为:

I ran into an unpredicted behavior with Python's Numpy, set and NaN (not-a-number):

>>> set([np.float64('nan'), np.float64('nan')])
set([nan, nan])
>>> set([np.float32('nan'), np.float32('nan')])
set([nan, nan])
>>> set([np.float('nan'), np.float('nan')])
set([nan, nan])
>>> set([np.nan, np.nan])
set([nan])
>>> set([float('nan'), float('nan')])
set([nan, nan])

此处np.nan产生一个元素集,而Numpy的nans在一个集合中产生多个nans. float('nan')也是如此!并注意:

Here np.nan yields a single element set, while Numpy's nans yield multiple nans in a set. So does float('nan')! And note that:

>>> type(float('nan')) == type(np.nan)
True

我想知道这种差异是如何产生的,以及不同行为背后的合理性.

I wonder how this difference come about and what the rationality is behind the different behaviors.

推荐答案

NAN的特性之一是NAN!= NAN,与所有其他数字不同.但是,set的实现首先在尝试插入新成员之前,先检查id(x)是否与哈希索引处的现有成员匹配.如果您有两个ID都不同的对象,它们的ID都不同,则您将在集合中获得两个条目.如果它们具有相同的ID,则它们会折叠为一个条目.

One of the properties of NAN is that NAN != NAN, unlike all other numbers. However, the implementation of set first checks to see if id(x) matches the existing member at a hash index before it tries to insert a new one. If you have two objects with different ids that both have the value NAN, you'll get two entries in the set. If they both have the same id, they collapse into a single entry.

正如其他人指出的那样,np.nan是一个始终具有相同ID的单个对象.

As pointed out by others, np.nan is a single object that will always have the same id.

这篇关于Python和Numpy的nan和set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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