📜  Python中的 max() 和 min()

📅  最后修改于: 2022-05-13 01:55:51.612000             🧑  作者: Mango

Python中的 max() 和 min()

本文为大家带来了一个非常有趣但鲜为人知的Python函数,即 max() 和 min()。现在,与它们的 C++ 对应物相比,它只允许两个参数,过于严格地是 float、int 或 char,这些函数不仅限于 2 个元素,而且可以保存许多元素作为参数,并且还支持参数中的字符串,因此允许按字典顺序显示最小或最大的字符串。下面解释详细的功能。

最大限度()

此函数用于计算在其参数中传递的值的最大值,如果字符串作为参数传递,则按字典顺序计算最大值。

Syntax : 
max(a,b,c,..,key,default)
Parameters : 
a,b,c,.. :  similar type of data.
key : key function where the iterables are passed and comparison is performed
default : default value is passed if the given iterable is empty
Return Value : 
Returns the maximum of all the arguments.
Exceptions : 
Returns TypeError when conflicting types are compared.

Python3
# Python code to demonstrate the working of
# max()
 
# printing the maximum of 4,12,43.3,19,100
print("Maximum of 4,12,43.3,19 and 100 is : ",end="")
print (max( 4,12,43.3,19,100 ) )


Python3
# Python code to demonstrate the working of
# min()
 
# printing the minimum of 4,12,43.3,19,100
print("Minimum of 4,12,43.3,19 and 100 is : ",end="")
print (min( 4,12,43.3,19,100 ) )


Python3
# Python code to demonstrate the Exception of
# min() and max()
 
# printing the minimum of 4,12,43.3,19, "GeeksforGeeks"
# Throws Exception
print("Minimum of 4,12,43.3,19 and GeeksforGeeks is : ",end="")
print (min( 4,12,43.3,19,"GeeksforGeeks" ) )


Python3
# Python code to demonstrate the Application of
# min() and max()
 
# printing the word occurring 1st among these in dict.
# "geeks", "manjeet", "algorithm", "programming"
print("The word occurring 1st in dict. among given is : ",end="")
print (min( "geeks", "manjeet", "algorithm", "programming" ) )
 
# printing the word occurring last among these in dict.
# "geeks", "manjeet", "algorithm", "programming"
print("The word occurring last in dict. among given is : ",end="")
print (max( "geeks", "manjeet", "algorithm", "programming" ) )


输出 :

Maximum of 4,12,43.3,19 and 100 is : 100

分钟()

此函数用于计算在其参数中传递的值的最小值,如果字符串作为参数传递,则按字典顺序计算最小值。

Syntax : 
min(a,b,c,.., key,default)
Parameters : 
a,b,c,.. :  similar type of data.
key : key function where the iterables are passed and comparison is performed
default : default value is passed if the given iterable is empty
Return Value : 
Returns the minimum of all the arguments.
Exceptions : 
Returns TypeError when conflicting types are compared.

Python3

# Python code to demonstrate the working of
# min()
 
# printing the minimum of 4,12,43.3,19,100
print("Minimum of 4,12,43.3,19 and 100 is : ",end="")
print (min( 4,12,43.3,19,100 ) )

输出 :

Minimum of 4,12,43.3,19 and 100 is : 4

例外

1. TypeError:这些函数在比较冲突的数据类型时会抛出 TypeError。

Python3

# Python code to demonstrate the Exception of
# min() and max()
 
# printing the minimum of 4,12,43.3,19, "GeeksforGeeks"
# Throws Exception
print("Minimum of 4,12,43.3,19 and GeeksforGeeks is : ",end="")
print (min( 4,12,43.3,19,"GeeksforGeeks" ) )

输出 :

Minimum of 4,12,43.3,19 and GeeksforGeeks is : 

运行时错误:

Traceback (most recent call last):
  File "/home/b5da1d7f834a267f94fbbefe1b31a83c.py", line 7, in 
    print (min( 4,12,43.3,19,"GeeksforGeeks" ) )
TypeError: unorderable types: str() < int()

实际应用

许多实际应用中的一个是查找按字典顺序排列的最大和最小的字符串,即字符串出现在字典中的第一个或最后一个。

Python3

# Python code to demonstrate the Application of
# min() and max()
 
# printing the word occurring 1st among these in dict.
# "geeks", "manjeet", "algorithm", "programming"
print("The word occurring 1st in dict. among given is : ",end="")
print (min( "geeks", "manjeet", "algorithm", "programming" ) )
 
# printing the word occurring last among these in dict.
# "geeks", "manjeet", "algorithm", "programming"
print("The word occurring last in dict. among given is : ",end="")
print (max( "geeks", "manjeet", "algorithm", "programming" ) )

输出 :

The word occurring 1st in dict. among given is : algorithm
The word occurring last in dict. among given is : programming