python中max()、min()获得最大值与最小值_(Python基础教程之十)Python max()和min()–在列表或数组中查找最大值和最小值...
使用**max()和min()**方法在可比較元素的集合(例如列表,集合或數(shù)組)中查找最大(或最小)項(xiàng)的Python示例。
1. Python max() function
max() 該功能用于–
計(jì)算在其參數(shù)中傳遞的最大值。
如果字符串作為參數(shù)傳遞,則在字典上的最大值。
1.1. Find largest integer in array
>>> nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
>>> max( nums )
42#Max value in array
1.2. Find largest string in array
>>> blogName = ["how","to","do","in","java"]
>>> max( blogName )
'to'#Largest value in array
1.3. Find max key or value
有點(diǎn)復(fù)雜的結(jié)構(gòu)。
>>> 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'
2. Python min() function
此功能用于–
計(jì)算在其參數(shù)中傳遞的最小值。
如果字符串作為參數(shù)傳遞,則在字典上的最小值。
2.1. Find lowest integer in array
>>> nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
>>> min( nums )
-4#Min value in array
2.2. Find smallest string in array
>>> blogName = ["how","to","do","in","java"]
>>> min( blogName )
'do'#Smallest value in array
2.3. Find min key or value
有點(diǎn)復(fù)雜的結(jié)構(gòu)。
>>> prices = {
'how': 45.23,
'to': 612.78,
'do': 205.55,
'in': 37.20,
'java': 10.75
}
>>> min( prices.values() )
10.75
>>> min( prices.keys() ) #or min( prices ). Default is keys().
'do'
學(xué)習(xí)愉快!
作者:分布式編程
出處:https://zthinker.com/
總結(jié)
以上是生活随笔為你收集整理的python中max()、min()获得最大值与最小值_(Python基础教程之十)Python max()和min()–在列表或数组中查找最大值和最小值...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 自动内存管理
- 下一篇: 企业家Scott Gerber:小公司应