Python學習筆記 - 最牛逼的內建函數max和min

來源:互聯網
上載者:User

標籤:style   lam   運行   最小   audio   迭代器   class   返回   key   

本文主要介紹了max的運行機制,以及如何傳入和比較,min函數的運行機制和max一樣,只是min取的是最小值

max/min 後直接跟序列會返回此序列的最大/最小值

max(iterable, key, default) 求迭代器的最大值,其中iterable 為迭代器,max會for i in … 遍曆一遍這個迭代器,然後將迭代器的每一個傳回值當做參數傳給key=func 中的func(一般用lambda運算式定義) ,然後將func的執行結果傳給key,然後以key為標準進行大小的判斷。

以下根據不同的判斷條件返回最大值也不同

d1 = {‘name‘: ‘egon‘, ‘price‘: 100}d2 = {‘name‘: ‘rdw‘, ‘price‘: 666}d3 = {‘name‘: ‘zat‘, ‘price‘: 1}l1 = [d1, d2, d3]a = max(l1, key=lambda x: x[‘name‘])print(a)b = max(l1, key=lambda x: x[‘price‘])print(b)

執行結果

{‘name‘: ‘zat‘, ‘price‘: 1}{‘name‘: ‘rdw‘, ‘price‘: 666}

Python學習筆記 - 最牛逼的內建函數max和min

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.