[英]The result of the Python max() function is confusing
When using Python max() function with the "key" argument and passing on this argument a function that calculates modulo of the given integer number I don't get desired results. 下面是代碼片段,兩個不同列表的結果由 integer 數字組成。
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))
結果分別為1和3 。 第一次打印的結果不應該是 3 嗎? 或者, max() function 本質上是否只給出了兩個相同數字的第一次出現? 兩個數字的 mod_2 操作的結果是相同的(因為 3 % 2 = 1 % 2)。 max() function 在這種情況下實際上是如何決定的?
當 Max function 給出兩個相同的數字時,它選擇第一個數字
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.