Internal min function of python [duplicate]

泄露秘密 提交于 2019-12-13 03:24:51

问题


I need to know how the min function of python works if there are more than one item in a list which are minimum. Which one takes min? Say A = [5, 3, 1, 4, 1]. Now if I say A.remove(min(A)) which one will be removed? The first 1 or the second 1?


回答1:


In this case it will remove the first one. It's more about the behavior of the list.remove function rather than the min function in this case. min just returns the lowest value in the list so it returns the integer 1. list.remove removes the leftmost instance of the parameter passed to it.

Also, as answered in this answer to the question that mgilson linked above, if you are dealing with objects rather than values (i.e. lists rather than integers), the first one matching the minimum value will be selected.



来源:https://stackoverflow.com/questions/17217500/internal-min-function-of-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!