Python built-in functions (43) -- min, python built-in 43 min

Source: Internet
Author: User

Python built-in functions (43) -- min, python built-in 43 min

English document:

min(Iterable,*[,Key,Default])

min(Arg1,Arg2,* Args[,Key])

Return the smallest item in an iterable or the smallest of two or more arguments.

If one positional argument is provided, it shocould be an iterable. The smallest item in the iterable is returned. If two or more positional arguments are provided, the smallest of the positional arguments is returned.

There are two optional keyword-only arguments.KeyArgument specifies a one-argument ordering function like that usedlist.sort().DefaultArgument specifies an object to return if the provided iterable is empty. If the iterable is empty andDefaultIs not provided,ValueErrorIs raised.

If multiple items are minimal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools suchsorted(iterable, key=keyfunc)[0]Andheapq.nsmallest(1, iterable, key=keyfunc).

 

Note:

  

1. The function is used to obtain the minimum values of multiple input parameters, or the minimum values of the input iteratable object elements. Default Value: Numeric parameter. The value is small. For numeric parameter, the former is used for alphabetic sorting. You can also pass in the named parameter key as a function to specify the method for getting the minimum value. The default naming parameter is used to specify the default value returned when the minimum value does not exist. Function is opposite to max function.

2. The function must pass in at least two parameters, but only one parameter is required. In this case, the parameter must be an Iterated object and the minimum element in the iterated object is returned.

>>> Min (1) # input 1 parameter error Traceback (most recent call last): File "<pyshell #0>", line 1, in <module> min (1) TypeError: 'int' object is not iterable> min (1, 2) # input 2 parameters take 2 smaller ones 1> min (1, 2, 3) # input 3 parameters take 3 smaller ones 1> min ('123 ') # input one iteratable object and obtain its minimum element value '1'

3. When the input parameters are of different data types, all input parameters are converted to implicit data types before comparison. If implicit data type conversion is not supported, an error is returned.

>>> Min (1, 1.1, 1.3e1) # The integer and floating point number can have a minimum value of 1 >>> min (, '3 ') # The value and string cannot take the minimum Traceback (most recent call last): File "<pyshell #5>", line 1, in <module> min (, '3 ') typeError: unorderable types: str () <int ()> min ([1, 2], [1, 3]) # The minimum value of list and list can be [1, 2] >>> min (1, 2), [1, 3]) # The list and tuples cannot take the minimum Traceback (most recent call last): File "<pyshell #7> ", line 1, in <module> min (1, 2), [1, 3]) TypeError: unorderable types: list () <tuple ()

4. If there are multiple identical minimum values, the minimum values that appear first are returned.

# Define three lists of a, B, and c> a = [1, 2]> B = [1, 3]> c = [1, 2] # view a, B, c id >>> id () 66486184 >>>> id (B) 66486224 >>> id (c) 66486024 # obtain the minimum value >>> d = min (a, B, c) >>> id (d) 66486184 # verify whether the minimum value is a >>> id (a) = id (d) True

5. Default Value: Numeric parameter. If the value is small, sorted by alphabet depends on the former. If the value is sequential, the minimum value is obtained based on the index value. You can also specify the minimum value method by passing in the name parameter key.

>>> Min (1, 2) # Take a small value 1 >>> min ('A', 'B ') # sort by the former 'A'> min ('AB', 'ac', 'ad ') # retrieve the smaller ones 'AB'> min (-1,-2) by index in sequence # Remove the smaller ones by default-2> min (-1, -2, key = abs) # If the absolute value function is input, the parameter will evaluate the absolute value and then take smaller ones-1

6. Another function of the key parameter is that objects of different types cannot be compared to the minimum value. If an appropriate key function is input, the minimum value can be compared.

>>> Min (, '3') # the minimum value of the value and string cannot be Traceback (most recent call last): File "<pyshell #22>", line 1, in <module> min (1, 2, '3') TypeError: unorderable types: str () <int ()> min (1, 2, '3', key = int) # After specifying the key as the conversion function, you can take the minimum value 1> min ([], () # The minimum value Traceback (most recent call last) cannot be used for tuples and lists ): file "<pyshell #24>", line 1, in <module> min ([1, 2], (1, 1) TypeError: unorderable types: tuple () <list ()> min ([], (), key = lambda x: x [1]) # After specifying the key as the element at the position 1 of the returned sequence index, minimum value (1, 1)

7. When only one iteratable object is passed in and the iteratable object is empty, you must specify the name parameter default to specify the default value returned by the function when the minimum value does not exist.

>>> Min () # null iteratable object cannot take the minimum Traceback (most recent call last): File "<pyshell #26>", line 1, in <module> min () ValueError: min () arg is an empty sequence >>> min (), default = 0) # empty iteratable object, specify the default parameter as the default value 0> min (), 0) # The default value must use the name parameter for parameter passing, otherwise it will be considered as a comparison element Traceback (most recent call last): File "<pyshell #27>", line 1, in <module> min (), 0) typeError: unorderable types: int () <tuple ()

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.