Python max() 函数的结果令人困惑

The result of the Python max() function is confusing

将 Python max() 函数与“key”参数一起使用并向该参数传递一个计算给定整数模数的函数时,我没有得到想要的结果。以下是代码片段,两个不同列表的结果由整数组成。

def mod_2(x):
    return x % 2

list1 = [1, 2, 3]
list2 = [3, 2, 1]

print(max(list1, key=mod_2))
print(max(list2, key=mod_2))

结果分别是13。第一次打印的结果不应该是 3 吗?或者, max() 函数是否天生就给出了两个相同数字的第一次出现?两个数字的 mod_2 运算结果相同(因为 3 % 2 = 1 % 2)。在这种情况下,max() 函数实际上是如何决定的?

你问:

does the max() function just give the first occurrence of the two same numbers by nature?

the documentation怎么说

If multiple items are maximal, the function returns the first one encountered.

不清楚?

当Max函数给出两个相同的数字时,它选择第一个数字