0

hi guys, I'm new to python and i can't seem to understand this one line of code right here:

distance, coordinates = min([(someFunction(state, coordinates), coordinates) for coordinates in coordinatesList])

coordinatesList contains a list of paired coordinates (x,y). someFunction returns an int. My question is why isn't distance and coordinates contain the same integer? i mean after the min() function, it should return an int, right? however, my print statements have coordinates as (someX, someY) and distance as some integer. How is this possible?

2
Contributors
2
Replies
28
Views
4 Years
Discussion Span
Last Post by plasticfood
3

Min function does not return an int.
Min function gets a sequence and returns the lowest element of that sequence.
In that case this sequence is a list of (distance, coordinates) pairs.

If you have a list of distance and coordinates like:
[(12,(1,2)),
(1,(12,25)),
(0,(1525,13))]
It will return the (0,(1525,13)) element, because the first value of this element (zero) is the lowest among the other elements.

So we have a statement:
distance, coordinates = (0,(1525,13))

Distance will be 0, and coordinates will be (1525,13). It is called sequence unpacking.

Look up the docs:
http://docs.python.org/2/library/functions.html#min
http://docs.python.org/2/tutorial/datastructures.html#comparing-sequences-and-other-types
http://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences

Votes + Comments
Shows knowledge!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.