问题
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