Python:最大/最小内置函数取决于参数顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4237914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: max/min builtin functions depend on parameter order
提问by max
max(float('nan'), 1)evaluates to nan
max(float('nan'), 1)评价为 nan
max(1, float('nan'))evaluates to 1
max(1, float('nan'))评估为 1
Is it the intended behavior?
这是预期的行为吗?
Thanks for the answers.
感谢您的回答。
maxraises an exception when the iterable is empty. Why wouldn't Python's maxraise an exception when nanis present? Or at least do something useful, like return nanor ignore nan. The current behavior is very unsafe and seems completely unreasonable.
max当可迭代对象为空时引发异常。为什么 Pythonmax在nan存在时不会引发异常?或者至少做一些有用的事情,比如 returnnan或 ignore nan。当前的行为非常不安全,似乎完全不合理。
I found an even more surprising consequence of this behavior, so I just posted a related question.
我发现了这种行为的更令人惊讶的后果,所以我刚刚发布了一个相关问题。
采纳答案by unutbu
In [19]: 1>float('nan')
Out[19]: False
In [20]: float('nan')>1
Out[20]: False
The float nanis neither bigger nor smaller than the integer 1.
maxstarts by choosing the first element, and only replaces it when it finds an element which is strictly larger.
浮点数nan既不大于也不小于整数1。
max从选择第一个元素开始,只有当它找到一个严格更大的元素时才替换它。
In [31]: max(1,float('nan'))
Out[31]: 1
Since nanis not larger than 1, 1 is returned.
由于nan不大于 1,因此返回 1。
In [32]: max(float('nan'),1)
Out[32]: nan
Since 1 is not larger than nan, nanis returned.
由于 1 不大于nan,nan因此返回。
PS. Note that np.maxtreats float('nan')differently:
附注。注意区别np.max对待float('nan'):
In [36]: import numpy as np
In [91]: np.max([1,float('nan')])
Out[91]: nan
In [92]: np.max([float('nan'),1])
Out[92]: nan
but if you wish to ignore np.nans, you can use np.nanmax:
但如果你想忽略np.nans,你可以使用np.nanmax:
In [93]: np.nanmax([1,float('nan')])
Out[93]: 1.0
In [94]: np.nanmax([float('nan'),1])
Out[94]: 1.0
回答by Katriel
I haven't seen this before, but it makes sense. Notice that nanis a very weird object:
我以前没见过这个,但这是有道理的。请注意,这nan是一个非常奇怪的对象:
>>> x = float('nan')
>>> x == x
False
>>> x > 1
False
>>> x < 1
False
I would say that the behaviour of maxis undefined in this case -- what answer would you expect? The only sensible behaviour is to assume that the operations are antisymmetric.
我会说max在这种情况下的行为是未定义的——你期望什么答案?唯一合理的行为是假设操作是反对称的。
Notice that you can reproduce this behaviour by making a broken class:
请注意,您可以通过创建一个损坏的类来重现此行为:
>>> class Broken(object):
... __le__ = __ge__ = __eq__ = __lt__ = __gt__ = __ne__ =
... lambda self, other: False
...
>>> x = Broken()
>>> x == x
False
>>> x < 1
False
>>> x > 1
False
>>> max(x, 1)
<__main__.Broken object at 0x024B5B50>
>>> max(1, x)
1
回答by terminus
Max works the following way:
Max 的工作方式如下:
The first item is set as maxval and then the next is compared to this value. The comparation will always return False:
第一项设置为 maxval,然后将下一项与此值进行比较。比较将始终返回 False:
>>> float('nan') < 1
False
>>> float('nan') > 1
False
So if the first value is nan, then (since the comparation returns false) it will not be replaced upon the next step.
所以如果第一个值是 nan,那么(因为比较返回 false)它不会在下一步被替换。
OTOH if 1 is the first, the same happens: but in this case, since 1 was set, it will be the maximum.
OTOH 如果 1 是第一个,也会发生同样的情况:但在这种情况下,由于设置了 1,它将是最大值。
You can verify this in the python code, just look up the function min_max in Python/bltinmodule.c
你可以在 python 代码中验证这一点,只需在 Python/bltinmodule.c 中查找函数 min_max

