Which maximum does Python pick in the case of a tie?

后端 未结 5 1844
盖世英雄少女心
盖世英雄少女心 2020-11-27 16:27

When using the max() function in Python to find the maximum value in a list (or tuple, dict etc.) and there is a tie for maximum value, which one does Python pi

5条回答
  •  半阙折子戏
    2020-11-27 16:45

    For Python 3, the behavior of max() in the case of ties is no longer just an implementation detail as detailed in the other answers. The feature is now guaranteed, as the Python 3 docs explicitly state:

    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] and heapq.nlargest(1, iterable, key=keyfunc).

提交回复
热议问题