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.
就像我們對 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.
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 中最大的值.
Here is one we can callmax()A list of floating point numbers for the function.in all floating point numbers,max()發現105.8是最大的.
This simple example just finds the maximum value in a series of numbers passed to the function.
Use on a dictionarymax()函數,返回字典中最大的鍵.
如果一個 dictionary 的鍵是字符串,max() The function returns the largest string in alphabetical order.
You shouldn't be calling on an empty list max() .如果你這樣做,就會拋出一個異常.為了解決這個問題,You can pass a default value,Used when the list is empty.
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()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) .