close
Skip to main content
added 252 characters in body
Source Link
John La Rooy
  • 306.7k
  • 54
  • 379
  • 517

Suppose you have

>>> seq = range(-4,4)
>>> def f(x):
...  return x*x-2

for the minimum value of f

>>> min(f(x) for x in seq)
-2

for the value of x at the minimum

>>> min(seq, key=f)
0

of course you can use lambda too

>>> min((lambda x:x*x-2)(x) for x in range(-4,4))
-2

but that is a little ugly, map looks better here

>>> min(map(lambda x:x*x-2, seq))
-2

>>> min(seq,key=lambda x:x*x-2)
0

Suppose you have

>>> seq = range(-4,4)
>>> def f(x):
...  return x*x-2

for the minimum value of f

>>> min(f(x) for x in seq)
-2

for the value of x at the minimum

>>> min(seq, key=f)
0

Suppose you have

>>> seq = range(-4,4)
>>> def f(x):
...  return x*x-2

for the minimum value of f

>>> min(f(x) for x in seq)
-2

for the value of x at the minimum

>>> min(seq, key=f)
0

of course you can use lambda too

>>> min((lambda x:x*x-2)(x) for x in range(-4,4))
-2

but that is a little ugly, map looks better here

>>> min(map(lambda x:x*x-2, seq))
-2

>>> min(seq,key=lambda x:x*x-2)
0
Source Link
John La Rooy
  • 306.7k
  • 54
  • 379
  • 517

Suppose you have

>>> seq = range(-4,4)
>>> def f(x):
...  return x*x-2

for the minimum value of f

>>> min(f(x) for x in seq)
-2

for the value of x at the minimum

>>> min(seq, key=f)
0
lang-py