Python min()函数
Python min()函数返回作为其参数传递的可迭代对象中的最小值或最小项。有两种类型的 min函数-
- 带有对象的 min() 函数
- 具有可迭代的 min() 函数
带有对象的 min() 函数
与 C/C++ 的 min()函数不同, Python中的 min()函数可以接受任何类型的对象并返回其中最小的。在字符串的情况下,它返回按字典顺序排列的最小值。
Syntax: min(a, b, c, …, key=func)
Parameters:
a, b, c, .. : similar type of data.
key: A function to customize the sort order
例子:
Python3
# Python code to demonstrate the
# working of min()
# printing the minimum of
# 4, 12, 43.3, 19, 100
print(min(4, 12, 43.3, 19, 100))
# printing the minimum of
# a, b, c, d, e
print(min('a', 'b', 'c', 'd', 'e'))
Python3
# Python code to demonstrate the
# working of min()
# find the string with minimum
# length
s = min("GfG", "Geeks", "GeeksWorld", key = len)
print(s)
Python3
# Python code to demonstrate the
# Exception of min()
# printing the minimum of 4, 12, 43.3, 19,
# "GeeksforGeeks" Throws Exception
print(min(4, 12, 43.3, 19, "GeeksforGeeks"))
Python3
# Python code to demonstrate the
# working of min()
# printing the minimum of [4, 12, 43.3, 19]
print(min([4, 12, 43.3, 19]))
# printing the minimum of "GeeksforGeeks"
print(min("GeeksforGeeks"))
# printing the minimum of ("A", "b", "C")
print(min(("A", "a", "C")))
Python3
# Python code to demonstrate the
# working of min()
d = {1: "c", 2: "b", 3: "a"}
# printing the minimum key of
# dictionary
print(min(d))
# printing the key with minimum
# value in dictionary
print(min(d, key = lambda k: d[k]))
Python3
# Python code to demonstrate the
# Exception of min()
L = []
# printing the minimum empty list
print(min(L))
输出:
4
a
自定义排序顺序
在 min()函数中传递自定义排序顺序键参数。
例子:
Python3
# Python code to demonstrate the
# working of min()
# find the string with minimum
# length
s = min("GfG", "Geeks", "GeeksWorld", key = len)
print(s)
输出:
GfG
引发异常
比较冲突的数据类型时,min() 函数会抛出TypeError 。
例子:
Python3
# Python code to demonstrate the
# Exception of min()
# printing the minimum of 4, 12, 43.3, 19,
# "GeeksforGeeks" Throws Exception
print(min(4, 12, 43.3, 19, "GeeksforGeeks"))
输出:
TypeError: unorderable types: str() < int()
具有可迭代的 min() 函数
当一个可迭代对象被传递给 min函数时,它返回可迭代对象的最小项。
Syntax: min(iterable, default = obj, key = func)
Parameters:
iterable: An iterable like list, tuple, string
default: Default value that is returned when the iterable is empty
key: A function to customize the sort order
例子:
Python3
# Python code to demonstrate the
# working of min()
# printing the minimum of [4, 12, 43.3, 19]
print(min([4, 12, 43.3, 19]))
# printing the minimum of "GeeksforGeeks"
print(min("GeeksforGeeks"))
# printing the minimum of ("A", "b", "C")
print(min(("A", "a", "C")))
输出:
4
G
A
自定义排序顺序
如上所示,自定义排序顺序的关键参数是在 min()函数中传递的。
例子:
Python3
# Python code to demonstrate the
# working of min()
d = {1: "c", 2: "b", 3: "a"}
# printing the minimum key of
# dictionary
print(min(d))
# printing the key with minimum
# value in dictionary
print(min(d, key = lambda k: d[k]))
输出:
1
3
引发异常
ValueError在没有默认参数的情况下传递空的可迭代对象时引发
例子:
Python3
# Python code to demonstrate the
# Exception of min()
L = []
# printing the minimum empty list
print(min(L))
输出:
ValueError: min() arg is an empty sequence