I don't understand the role of key in these functions
Question:
I’m not sure why the bottom result is returning a tuple of (3,3)
no matter what
>>> points = [(0, 0), (1, 4), (3, 3), (4, 0)]
>>> max(points)
(4, 0)
>>> max(points, key=lambda p: p[0])
(4, 0)
>>> max(points, key=lambda p: p[1])
(1, 4)
>>> max(points, key=lambda p: p[0]**2 + p[1]**2)
(3, 3)
>>> max(points, key=lambda p: p[0]**2*2 + p[1]**2*2)
(3, 3)
Im not sure what key is doing here with regards to, what it seems to be doing is squaring the tuples, and then returning (3,3)
.
Would someone be able to shed a little light on this? The material I’m reading mentions
that key is charged with calculating a numeric value for each parameter
Le callable key
est donc chargé de calculer une valeur numérique pour chacun des paramètres
and it is then able to compare parameters. However that doesn’t explain why the same tuple is appearing. i.e
max(points, key=lambda p: (p[0]**2*2 + p[1]**2*2)+2)
Answers:
key
is a parameter that takes in a method (the lambda
part). The method itself takes in a tuple p
, and, in the third example, returns the sum of the squares of the components of p
.
Mapping each tuple in points
to that value for each, we have [(0, 0), (1, 4), (3, 3), (4, 0)]
with values of [0, 17, 18, 16]
respectively. Since 18
is the greatest value, (3, 3)
is the "max" using that key to evaluate the points (as that is the point that corresponds to 18
).
Using the fourth example, we have similar logic. The corresponding values are [0, 34, 36, 32]
. Again, since 36
is greatest, (3, 3)
is the max.
I’m not sure why the bottom result is returning a tuple of (3,3)
no matter what
>>> points = [(0, 0), (1, 4), (3, 3), (4, 0)]
>>> max(points)
(4, 0)
>>> max(points, key=lambda p: p[0])
(4, 0)
>>> max(points, key=lambda p: p[1])
(1, 4)
>>> max(points, key=lambda p: p[0]**2 + p[1]**2)
(3, 3)
>>> max(points, key=lambda p: p[0]**2*2 + p[1]**2*2)
(3, 3)
Im not sure what key is doing here with regards to, what it seems to be doing is squaring the tuples, and then returning (3,3)
.
Would someone be able to shed a little light on this? The material I’m reading mentions
that key is charged with calculating a numeric value for each parameter
Le callable
key
est donc chargé de calculer une valeur numérique pour chacun des paramètres
and it is then able to compare parameters. However that doesn’t explain why the same tuple is appearing. i.e
max(points, key=lambda p: (p[0]**2*2 + p[1]**2*2)+2)
key
is a parameter that takes in a method (the lambda
part). The method itself takes in a tuple p
, and, in the third example, returns the sum of the squares of the components of p
.
Mapping each tuple in points
to that value for each, we have [(0, 0), (1, 4), (3, 3), (4, 0)]
with values of [0, 17, 18, 16]
respectively. Since 18
is the greatest value, (3, 3)
is the "max" using that key to evaluate the points (as that is the point that corresponds to 18
).
Using the fourth example, we have similar logic. The corresponding values are [0, 34, 36, 32]
. Again, since 36
is greatest, (3, 3)
is the max.