Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyword functions for Python min/max

Tags:

python

key

min

I am trying to understand how this works:

my_dict = {'a':2,'b':1}
min(my_dict, key=my_dict.get)

produces

b

Which is a really cool feature and one I want to understand better.
Based on the documentation

min(iterable[, key]) Return the smallest item in an iterable or the smallest of two or more arguments... The optional key argument specifies a one-argument ordering function like that used for list.sort(). The key argument, if supplied, must be in keyword form (for example, min(a,b,c,key=func)).

Where can I find out more about available functions? In the case of a dictionary, is it all the dictionary methods?

Edit: I came across this today:

max(enumerate(array_x), key=operator.itemgetter(1))

Still looking for information on available keyword functions for min/max

like image 326
DanGoodrick Avatar asked Apr 08 '16 14:04

DanGoodrick


People also ask

Is there a MIN MAX function in Python?

Use Python's min() and max() to find smallest and largest values in your data. Call min() and max() with a single iterable or with any number of regular arguments.

Is Min a keyword in Python?

The Python min() function can take any iterable and returns the smallest item in an iterable. iterable: Any Python iterable (ex.: list, tuple, set, dict, etc.) key (optional): function to customize the sorting of items in an iterable.

Is Max a keyword in Python?

Python max() FunctionThe max() function returns the item with the highest value, or the item with the highest value in an iterable. If the values are strings, an alphabetically comparison is done.

What is the function of MAX () and MIN ()?

SQL MIN() and MAX() FunctionsThe MIN() function returns the smallest value of the selected column. The MAX() function returns the largest value of the selected column.


1 Answers

The code you have written is

my_dict = {'a':2,'b':1}
min(my_dict, key=my_dict.get)

actually this works on min function. so, what does min do?

min(a, b, c, ...[, key=func]) -> value

With a single iterable argument, return its lowest item. With two or more arguments, return the lowest argument.

The key here is used to pass a custom comparison function.

Example: output max by length of list, where arg1, arg2 are both lists.

>>>> max([1,2,3,4], [3,4,5], key=len)
[1, 2, 3, 4]

But what if I want the max from the list, but by considering the second element of the tuple? here we can use functions, as given in official documentation. The def statements are compound statements they can't be used where an expression is required, that's why sometimes lambda's are used.

Note that lambda is equivalent to what you'd put in a return statement of a def. Thus, you can't use statements inside a lambda, only expressions are allowed.

>>> max(l, key = lambda i : i[1])
(1, 9)

# Or

>>> import operator
>>> max(l, key = operator.itemgetter(1))
(1, 9)

so the functions are basically depend upon the the iterable and and passing the criteria for the comparison.

Now in your example, you are iterating over your dictionary. And in key, you are using get method here.

The method get() returns a value for the given key. If key is not available then returns default value None.

As here, no arguments are there in get method it simply iterates over values of dictionary. And thus the min gives you the key having minimum value.

For max(enumerate(array_x), key=operator.itemgetter(1)) we want to compare the values of array instead of their indices. So we have enumerated the array.

enumerate(thing), where thing is either an iterator or a sequence, returns a iterator that will return (0, thing[0]), (1, thing1), (2, thing[2])

now we have used itemgetter function of operator module. operator.itemgetter(n) constructs a callable that assumes an iterable object (e.g. list, tuple, set) as input, and fetches the n-th element out of it.

you can also use lambda function of here like

max(enumerate(array_x), key=lambda i: i[1])

So the range of functions in key is almost up to the use. we can use many functions but the sole motive is , it is the criteria for that comparison.

like image 197
7bStan Avatar answered Oct 20 '22 01:10

7bStan