Why will python function max() return different outputs if float('NaN') value is permuted in a dictionary but key-max_value remains the same?

huangapple go评论54阅读模式
英文:

Why will python function max() return different outputs if float('NaN') value is permuted in a dictionary but key-max_value remains the same?

问题

让我们假装我有以下简单的字典:

dictionary = {'a': 3, 'b': 4, 'c': float('NaN')}

如果我使用max()函数来返回具有最大值的键:

key_maxvalue = max(dictionary, key=dictionary.get)
print(key_maxvalue)

Python 输出如下:

b

然而,当我对键'a'和'c'的值进行排列时:

dictionary = {'a': float('NaN'), 'b': 4, 'c': 3}
key_maxvalue = max(dictionary, key=dictionary.get)
print(key_maxvalue)

我得到了这个意外的结果:

a

我原本期望Python会输出'b',因为该键仍然在字典中具有最大值。为什么值的顺序变化会影响max()函数的输出?此外,如何防止这种(意外的)情况发生?

英文:

Let's pretend I have the following simple dictionary:

dictionary = {'a':3, 'b':4, 'c':float('NaN')}

If I use function max() to return the key with maximum value...

key_maxvalue = max(dictionary, key=dictionary.get)
print(key_maxvalue)

...python outputs this:

b

However, when I permute the values of keys 'a' and 'c'...

dictionary = {'a':float('NaN'), 'b':4, 'c':3}
key_maxvalue = max(dictionary, key=dictionary.get)
print(key_maxvalue)

...I get this unexpected result:

a

I expected python would output 'b', as that key still has the maximum value in the dictionary. Why has a change in the values order altered the function max() output? Furthermore, how could I prevent this (unexpected) event from happening?

答案1

得分: 2

答案是,“不要使用 NaN”。NaN 的要点是它不是一个数字,不能依赖它以任何合理的方式像数字一样运作。你所看到的是与 NaN 的比较不是可交换的。

请注意这个例子:

Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type "help", "copyright", "credits", or "license" for more information.
>>> x = float('NaN')
>>> 1 < x
False
>>> x < 1
False
>>> 

与 NaN 的每次比较都是 false。这使得对它们进行排序是不确定的。

英文:

The answer is, "don't use NaN". The point of an NaN is that it is not a number, and cannot be relied on to act like a number in any rational way. What you're seeing is that comparisons with NaN are not commutative.

Notice this:

Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; x = float(&#39;NaN&#39;)
&gt;&gt;&gt; 1 &lt; x
False
&gt;&gt;&gt; x &lt; 1
False
&gt;&gt;&gt; 

Every comparison with a NaN is false. That makes sorting them indeterminate.

答案2

得分: 2

以下是翻译好的部分:

如果你编写了自己的函数,它可能会看起来像这样:

def max(nums):
    largest = nums[0]

    for item in nums:
        if item &gt; largest:
            largest = item

    return largest

问题在于这个比较 item &gt; largest。看看当你将一个数字与 np.nan 进行比较时会发生什么。

输入:np.nan &gt; 4

输出:False

输入:4 &gt; np.nan

输出:False

NaN 的任何比较都将为False。如果 max 函数类似于我们编写的函数,那么它会发生在你两种情况中都发生的情况。它不大于4,所以 b 仍然是最大值。但是,当它默认为第二种情况中的 a 时,没有其他数字大于 NaN,所以 a 仍然是最大值。

英文:

If you wrote your own function, it might look like this:

def max(nums):
    largest = nums[0]

    for item in nums:
        if item &gt; largest:
            largest = item

    return largest

The problem is this comparison item &gt; largest. Look what happens when you compare a number with np.nan.

Input: np.nan &gt; 4

Output: False

Input: 4 &gt; np.nan

Output: False

Any comparison with a NaN will be False. If max functions like our written function, then it happens what happens in both of your cases. It's not larger than 4, so b is still the max. However, when it defaults to a in the second case, no other number is larger than NaN, so a remains the max.

huangapple
  • 本文由 发表于 2023年4月4日 04:13:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定