英文:
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 "help", "copyright", "credits" or "license" for more information.
>>> x = float('NaN')
>>> 1 < x
False
>>> x < 1
False
>>>
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 > largest:
largest = item
return largest
问题在于这个比较 item > largest
。看看当你将一个数字与 np.nan
进行比较时会发生什么。
输入:np.nan > 4
输出:False
输入:4 > 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 > largest:
largest = item
return largest
The problem is this comparison item > largest
. Look what happens when you compare a number with np.nan
.
Input: np.nan > 4
Output: False
Input: 4 > 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论