本文介绍了为什么collections.Counter将numpy.nan视为相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下行为感到惊讶:

I am surprised by the following behavior:

>>> import numpy as np
>>> from collections import Counter
>>> my_list = [1,2,2, np.nan, np.nan]
>>> Counter(my_list)
Counter({nan: 2, 2: 2, 1: 1}) # Counter treats np.nan as equal and
                              # tells me that I have two of them
>>> np.nan == np.nan          # However, np.nan's are not equal
False

这是怎么回事?

当我使用float('nan')而不是np.nan时,我得到了预期的行为:

When I use float('nan') instead of np.nan, I get the behavior I expect:

>>> my_list = [1,2,2, float('nan'), float('nan')]
>>> Counter(my_list)
Counter({2: 2, nan: 1, 1: 1, nan: 1}) # two different nan's
>>> float('nan') == float('nan')
False

我正在使用python 2.7.3numpy 1.8.1.

如果我这样做:

>>> a = 300
>>> b = 300
>>> a is b
False
>>> Counter([a, b])
Counter({300: 2})

因此,Counter或任何python dict会在以下情况下将两个对象XY视为不相同:

So, Counter or any python dict considers two objects X and Y not the same if:

X == Y -> False

and

X is Y -> False

正确吗?

推荐答案

这与numpy.nanfloat("nan")无关,因为您有两个单独的float nan.

This isn't about numpy.nan vs. float("nan"), it's that you've got two separate float nans.

>>> np.nan is np.nan
True
>>> float("nan") is float("nan")
False

等等

>>> Counter([1,2,2, np.nan, np.nan])
Counter({nan: 2, 2: 2, 1: 1})
>>> Counter([1,2,2, float("nan"), float("nan")])
Counter({2: 2, nan: 1, 1: 1, nan: 1})

但是

>>> f = float("nan")
>>> Counter([1,2,2, f, f])
Counter({nan: 2, 2: 2, 1: 1})

这篇关于为什么collections.Counter将numpy.nan视为相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 03:07