亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python max內(nèi)置函數(shù)詳細(xì)介紹

 更新時間:2016年11月17日 15:21:14   投稿:lqh  
這篇文章主要介紹了Python MAX內(nèi)置函數(shù)詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下

Python max內(nèi)置函數(shù)

max(iterable, *[, key, default])

max(arg1, arg2, *args[, key])

Return the largest item in an iterable or the largest of two or more arguments.

If one positional argument is provided, it should be an iterable. The largest item in the iterable is returned. If two or more positional arguments are provided, the largest 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 maximal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as sorted(iterable, key=keyfunc, reverse=True)[0] and heapq.nlargest(1, iterable, key=keyfunc).

 說明:

  1. 函數(shù)功能為取傳入的多個參數(shù)中的最大值,或者傳入的可迭代對象元素中的最大值。默認(rèn)數(shù)值型參數(shù),取值大者;字符型參數(shù),取字母表排序靠后者。還可以傳入命名參數(shù)key,其為一個函數(shù),用來指定取最大值的方法。default命名參數(shù)用來指定最大值不存在時返回的默認(rèn)值。

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

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

  3. 當(dāng)傳入?yún)?shù)為數(shù)據(jù)類型不一致時,傳入的所有參數(shù)將進(jìn)行隱式數(shù)據(jù)類型轉(zhuǎn)換后再比較,如果不能進(jìn)行隱式數(shù)據(jù)類型轉(zhuǎn)換,則會報錯。

>>> max(1,1.1,1.3E1) # 整數(shù)與浮點(diǎn)數(shù)可取最大值
13.0
>>> max(1,2,3,'3') # 數(shù)值與字符串不能取最大值
Traceback (most recent call last):
 File "<pyshell#5>", line 1, in <module>
  max(1,2,3,'3')
TypeError: unorderable types: str() > int()

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

  4. 當(dāng)存在多個相同的最大值時,返回的是最先出現(xiàn)的那個最大值。

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

#查看a、b、c 的id
>>> id(a)
68128320
>>> id(b)
68128680
>>> id(c)
68128240

#取最大值
>>> d = max(a,b,c)
>>> id(d)
68128320

#驗(yàn)證是否最大值是否是a
>>> id(a) == id(d)
True

  5. 默認(rèn)數(shù)值型參數(shù),取值大者;字符型參數(shù),取字母表排序靠后者;序列型參數(shù),則依次按索引位置的值進(jìn)行比較取最大者。還可以通過傳入命名參數(shù)key,指定取最大值方法。

>>> max(1,2) # 取數(shù)值大者
2
>>> max('a','b') # 取排序靠后者
'b'
>>> max('ab','ac','ad') # 依次按索引比較取較大者
'ad'

>>> max(-1,0) # 數(shù)值默認(rèn)去數(shù)值較大者
0
>>> max(-1,0,key = abs) # 傳入了求絕對值函數(shù),則參數(shù)都會進(jìn)行求絕對值后再取較大者
-1

  6. key參數(shù)的另外一個作用是,不同類型對象本來不能比較取最大值的,傳入適當(dāng)?shù)膋ey函數(shù),變得可以比較能取最大值了。

>>> max(1,2,'3') #數(shù)值和字符串不能取最大值
Traceback (most recent call last):
 File "<pyshell#21>", line 1, in <module>
  max(1,2,'3')
TypeError: unorderable types: str() > int() 
>>> max(1,2,'3',key = int) # 指定key為轉(zhuǎn)換函數(shù)后,可以取最大值
'3'

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

7. 當(dāng)只傳入的一個可迭代對象時,而且可迭代對象為空,則必須指定命名參數(shù)default,用來指定最大值不存在時,函數(shù)返回的默認(rèn)值。

>>> max(()) #空可迭代對象不能取最大值
Traceback (most recent call last):
 File "<pyshell#26>", line 1, in <module>
  max(())
ValueError: max() arg is an empty sequence
>>> max((),default=0) #空可迭代對象,指定default參數(shù)為默認(rèn)值
0
>>> max((),0) #默認(rèn)值必須使用命名參數(shù)進(jìn)行傳參,否則將被認(rèn)為是一個比較的元素
Traceback (most recent call last):
 File "<pyshell#27>", line 1, in <module>
  max((),0)
TypeError: unorderable types: int() > tuple()

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • Python爬蟲lxml庫處理XML和HTML文檔

    Python爬蟲lxml庫處理XML和HTML文檔

    在當(dāng)今信息爆炸的時代,網(wǎng)絡(luò)上的數(shù)據(jù)量龐大而繁雜,為了高效地從網(wǎng)頁中提取信息,Python爬蟲工程師們需要強(qiáng)大而靈活的工具,其中,lxml庫憑借其卓越的性能和豐富的功能成為Python爬蟲領(lǐng)域的不可或缺的工具之一,本文將深入介紹lxml庫的各個方面,充分掌握這個強(qiáng)大的爬蟲利器
    2023-12-12
  • Python3.6簡單操作Mysql數(shù)據(jù)庫

    Python3.6簡單操作Mysql數(shù)據(jù)庫

    這篇文章主要為大家詳細(xì)介紹了Python3.6簡單操作Mysql數(shù)據(jù)庫,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Python中的類的定義和對象的創(chuàng)建方法

    Python中的類的定義和對象的創(chuàng)建方法

    object?是?Python?為所有對象提供的?基類,提供有一些內(nèi)置的屬性和方法,可以使用?dir?函數(shù)查看,這篇文章主要介紹了Python中的類的定義和對象的創(chuàng)建,需要的朋友可以參考下
    2022-11-11
  • 在Python中存儲字符串

    在Python中存儲字符串

    這篇文章主要介紹了在Python中存儲字符串,文章通過unicode展開主題相關(guān)內(nèi)容,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-05-05
  • 解決python3中自定義wsgi函數(shù),make_server函數(shù)報錯的問題

    解決python3中自定義wsgi函數(shù),make_server函數(shù)報錯的問題

    下面小編就為大家分享一篇解決python3中自定義wsgi函數(shù),make_server函數(shù)報錯的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • 對python 中class與變量的使用方法詳解

    對python 中class與變量的使用方法詳解

    今天小編就為大家分享一篇對python 中class與變量的使用方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • python實(shí)現(xiàn)超時退出的三種方式總結(jié)

    python實(shí)現(xiàn)超時退出的三種方式總結(jié)

    這篇文章主要介紹了python實(shí)現(xiàn)超時退出的三種方式總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 一招教你解決Pytorch GPU版本安裝慢的問題

    一招教你解決Pytorch GPU版本安裝慢的問題

    Pytorch是一個流行的深度學(xué)習(xí)框架,廣泛應(yīng)用于計算機(jī)視覺、自然語言處理等領(lǐng)域,下面我們就來看看如何在Windows操作系統(tǒng)上安裝Pytorch GPU版本吧
    2025-03-03
  • Keras多線程機(jī)制與flask多線程沖突的解決方案

    Keras多線程機(jī)制與flask多線程沖突的解決方案

    這篇文章主要介紹了Keras多線程機(jī)制與flask多線程沖突的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python MySQL查詢限制方式詳解

    Python MySQL查詢限制方式詳解

    MySQL是一款廣泛使用的關(guān)系型數(shù)據(jù)庫,而Python是一門流行的編程語言,在進(jìn)行數(shù)據(jù)庫操作時,二者的結(jié)合可以幫助我們更加高效地進(jìn)行操作和管理,這篇文章主要介紹了Python MySQL查詢限制,需要的朋友可以參考下
    2023-11-11

最新評論