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

后端 未结 5 1838
盖世英雄少女心
盖世英雄少女心 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 17:12

    Your question somewhat leads to a note. When sorting a data structure, there is often a desire to keep relative order of objects that are considered equal for the purposes of comparison. This would be known as a stable sort.

    If you absolutely needed this feature, you could do a sort(), which will be stable and then have knowledge of the order relative to the original list.

    As per python itself, I don't believe that you get any guarantee of which element you will get when you call max(). Other answers are giving the cpython answer, but other implementations (IronPython, Jython) could function differently.

提交回复
热议问题