Python max() 函数怎么用

1,364次阅读
没有评论
Python

与max函数经常一起出现的是min函数,对于这两个函数,因为长得差不多,使得经常呗搞混,要如何弄清这两个函数呢?一起来看下把~

示例演示:

Python max() function

max() 该功能用于–

计算在其参数中传递的最大值。

如果字符串作为参数传递,则在字典上的最大值。

Find largest integer in array

>>> nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
>>> max( nums )
42        #Max value in array

Find largest string in array

>>> blogName = ["how","to","do","in","java"]
>>> max( blogName )
'to'        #Largest value in array

Find max key or value

有点复杂的结构。

>>> prices = {
   'how': 45.23,
   'to': 612.78,
   'do': 205.55,
   'in': 37.20,
   'java': 10.75
}
 
>>> max( prices.values() )
612.78
 
>>> max( prices.keys() )     #or max( prices ). Default is keys().
'to'

好了,以上就是关于max函数的使用了。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-06-06发表,共计617字。
新手QQ群:570568346,欢迎进群讨论 Python51学习