Python內置函數(43)——min


英文文檔:

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 should 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. The key argument specifies a one-argument ordering function like that used for list.sort(). The default argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, a ValueError is raised.

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

 

說明:

  

  1. 函數功能為取傳入的多個參數中的最小值,或者傳入的可迭代對象元素中的最小值。默認數值型參數,取值小者;字符型參數,取字母表排序靠前者。還可以傳入命名參數key,其為一個函數,用來指定取最小值的方法。default命名參數用來指定最小值不存在時返回的默認值。功能與max函數相反。

  2. 函數至少傳入兩個參數,但是有只傳入一個參數的例外,此時參數必須為可迭代對象,返回的是可迭代對象中的最小元素。

>>> min(1) # 傳入1個參數報錯
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    min(1)
TypeError: 'int' object is not iterable
>>> min(1,2) # 傳入2個參數 取2個中較小者
1
>>> min(1,2,3) # 傳入3個參數 取3個中較小者
1
>>> min('1234') # 傳入1個可迭代對象,取其最小元素值
'1'

  3. 當傳入參數為數據類型不一致時,傳入的所有參數將進行隱式數據類型轉換后再比較,如果不能進行隱式數據類型轉換,則會報錯。

>>> min(1,1.1,1.3e1) # 整數與浮點數可取最小值
1
>>> min(1,2,'3') # 數值與字符串不能取最小值
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    min(1,2,'3')
TypeError: unorderable types: str() < int()

>>> min([1,2],[1,3]) # 列表與列表可取最小值
[1, 2]
>>> min((1,2),[1,3]) # 列表與元組不能取最小值
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    min((1,2),[1,3])
TypeError: unorderable types: list() < tuple()

   4. 當存在多個相同的最小值時,返回的是最先出現的那個最小值。

#定義a、b、c 3個列表
>>> a = [1,2]
>>> b = [1,3]
>>> c = [1,2]

#查看a、b、c 的id
>>> id(a)
66486184
>>> id(b)
66486224
>>> id(c)
66486024

#取最小值
>>> d = min(a,b,c)
>>> id(d)
66486184


#驗證是否最小值是否是a
>>> id(a) == id(d)
True

  5. 默認數值型參數,取值小者;字符型參數,取字母表排序靠前者;序列型參數,則依次按索引位置的值進行比較取最小者。還可以通過傳入命名參數key,指定取最小值方法。

>>> min(1,2) # 取數值小者
1
>>> min('a','b') # 取排序靠前者
'a'
>>> min('ab','ac','ad') # 依次按索引比較取較小者
'ab'

>>> min(-1,-2) # 數值默認去數值較小者
-2
>>> min(-1,-2,key = abs)  # 傳入了求絕對值函數,則參數都會進行求絕對值后再取較小者
-1

  6. key參數的另外一個作用是,不同類型對象本來不能比較取最小值的,傳入適當的key函數,變得可以比較能取最小值了。

>>> min(1,2,'3') #數值和字符串不能取最小值
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) # 指定key為轉換函數后,可以取最小值
1

>>> min([1,2],(1,1)) #元組和列表不能取最小值
Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    min([1,2],(1,1))
TypeError: unorderable types: tuple() < list()
>>> min([1,2],(1,1),key = lambda x:x[1]) #指定key為返回序列索引1位置的元素后,可以取最小值
(1, 1)

  7. 當只傳入的一個可迭代對象時,而且可迭代對象為空,則必須指定命名參數default,用來指定最小值不存在時,函數返回的默認值。

>>> min(()) #空可迭代對象不能取最小值
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    min(())
ValueError: min() arg is an empty sequence

>>> min((),default = 0) #空可迭代對象,指定default參數為默認值
0

>>> min((),0) #默認值必須使用命名參數進行傳參,否則將被認為是一個比較的元素
Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    min((),0)
TypeError: unorderable types: int() < tuple()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM