max獲取最大值時,若給定的值如果為空蛾洛,且未給max初始默認(rèn)值养铸,max會拋出異常
a = []
max(a)
拋出的異常
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-31-7369ceda7e26> in <module>
1 a = []
----> 2 max(a)
ValueError: max() arg is an empty sequence
未防止max拋出異常,可指定默認(rèn)值
a = []
max(a,default=False)
結(jié)果為False
False
max(iterable, *[, key, default])
max(arg1, arg2, *args[, key])
Return the largest item in an iterable or the largest of two or more arguments.
If one positional argument is provided, it should be an iterable. The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments is returned.
There are two optional keyword-only arguments. The key argument specifies a one-argument ordering function like that used for list.sort(). The default argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, a ValueError is raised.
If multiple items are maximal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as sorted(iterable, key=keyfunc, reverse=True)[0] andheapq.nlargest(1, iterable, key=keyfunc).
New in version 3.4: The default keyword-only argument.