程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Example of the max() function in Python

編輯:Python

Another is useful when analyzing data Python 內置函數是 max().It works similarly to what we just coveredmin(),But arguably the opposite.It's not looking for the smallest item,Instead, it looks for the largest item.就像min()一樣,It can accept a sequence as input,A series of two or more items can also be accepted as input.when given a sequence,max()Returns the largest item in the sequence.When a sequence of two or more items is provided,max()Returns the largest item in it.現在讓我們看看max()在PythonA few examples of how it works.


Python max() 函數示例 1

就像我們對 min() 函數所做的那樣,We can first initialize a list of numbers to work with.

Now in order to find the largest item in the list,我們可以調用 max() 函數,And pass in the variable that holds the list of numbers.max()The function finds the integer correctly99as the largest one in the list of numbers.


Python max() 函數示例 2

max() A function works with primitives as it does with lists.The example here is one that contains all the same numbersTuple.max() The function reports again 99 This integer is this Tuple 中最大的值.


Python max() 函數示例 3

Here is one we can callmax()A list of floating point numbers for the function.in all floating point numbers,max()發現105.8是最大的.


Python max() 函數示例 4

This simple example just finds the maximum value in a series of numbers passed to the function.


Python max() 函數示例 5

Use on a dictionarymax()函數,返回字典中最大的鍵.


Python max() 函數示例 6

如果一個 dictionary 的鍵是字符串,max() The function returns the largest string in alphabetical order.


Python max() 函數示例 7

You shouldn't be calling on an empty list max() .如果你這樣做,就會拋出一個異常.為了解決這個問題,You can pass a default value,Used when the list is empty.


Python max() 函數示例 8

A custom key can be defined to decide max() 如何工作.This example shows finding strings(字母)Default algorithm for max value,Use a custom key to change it to use ASCII length to find the maximum value.


Python max() 函數摘要

Python max()The function returns the item with the highest value or the item with the highest value in an iterable of items.if the values ​​are strings,則按字母順序進行比較.The syntax for multiple items ismax(n1, n2, n3, ...)max(iterable)(If it is an iterable item) .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved