[Tutor] min() & mutable sequences
Thu, 20 Dec 2001 13:43:27 +0800
First to give some background: I have a list of instances, each has an
attributes "distance". I want to get the instance with the smallest
I have at present:-
robots=[...] #The ... is a whole lot of instances :)
closest=min([robot.distance for robot in robots])
Okay, I now have the closest *distance*.
Say more than one instance has the same distance, will min() return the
first instance with that smallest distance? Because if that is so, I could
then just get the related instance using:-
closest_index=[robot.distance for robot in robots].index(closest)
#Yeah, I used that list comprehension twice, this is just for demonstration,
Is there a better way to do it?
I guess I could iterate over the list, and keep a smallest distance variable
somewhere. I guess I've become somewhat of a list-comprehension-addict