1. <source id="nejs2"></source>
  2. <video id="nejs2"></video>
    <source id="nejs2"></source>
          1. Python max()

            更新:

            內置函數max()有助于 返回給定可迭代表中最大的元素。也可以在兩個或多個參數之間找到最大的元素。

             # to find the largest item in an iterable
            **max(iterable, *iterables, key, default)** 
            
             # to find the largest item between two or more objects
            **max(arg1, arg2, *args, key)** 
            

            最大()參數:

            帶有 iterable 的max()函數具有以下參數。

            *最大值(可重復,可重復,鍵,默認)**

            參數 描述 必需/可選
            可迭代的 諸如列表、元組、集合、字典等可條目。 需要
            *可重復 任意數量的可滴定物;可以不止一個 可選擇的
            傳遞數據項并進行比較的一種關鍵功能 可選擇的
            系統默認值 如果給定的 iterable 為空,則為默認值 可選擇的

            不帶iterable()iterable()函數有以下參數。

            *最大值(arg1、arg2、args、鍵)**

            參數 描述 必需/可選
            arg1 一個對象;可以是數字、字符串等 需要
            arg2 一個對象;可以是數字、字符串等 需要
            *參數 任意數量的對象 可選擇的
            傳遞每個參數并進行比較的一個關鍵函數 可選擇的

            最大()返回值

            在傳遞空迭代器的情況下,它會引發 ValueError 異常。為了避免這種情況,我們可以傳遞默認參數。 如果我們傳遞多個迭代器,那么從給定的迭代器中返回最大的項。

            | 投入 | 返回值 | | 整數 | 最大整數 | | 線 | 具有最大 Unicode 值的字符 |

            Python 中max()方法的示例

            示例 1:獲取列表中最大的項目

             number = [13, 2, 8, 5, 10, 26]
            largest_number = max(number);
            
            print("The largest number is:", largest_number) 
            

            輸出:

            The largest number is: 26 

            示例 2:查找列表中最大的字符串

             languages = ["Python", "C Programming", "Java", "JavaScript"]
            largest_string = max(languages);
            
            print("The largest string is:", largest_string) 
            

            輸出:

            The largest string is: Python 

            示例 3:在字典中查找max()

             square = {2: 4, -3: 9, -1: 1, -2: 4}
            
            # the largest key
            key1 = max(square)
            print("The largest key:", key1)    # 2
            
            # the key whose value is the largest
            key2 = max(square, key = lambda k: square[k])
            
            print("The key with the largest value:", key2)    # -3
            
            # getting the largest value
            print("The largest value:", square[key2])    # 9 
            

            輸出:

            The largest key: 2
            The key with the largest value: -3
            The largest value: 9 

            例 4:在給定的數字中找出最大值

             result = max(4, -5, 23, 5)
            print("The maximum number is:", result) 
            

            輸出:

            The maximum number is: 23 

            示例 5:查找對象的最大值()

             class Data:
                id = 0
            
                def __init__(self, i):
                    self.id = i
            
                def __str__(self):
                    return 'Data[%s]' % self.id
            
            def get_data_id(data):
                return data.id
            
            # max() with objects and key argument
            list_objects = [Data(1), Data(2), Data(-10)]
            
            print(max(list_objects, key=get_data_id)) 
            

            輸出:

            Data[2] 
            頂部 久久久久99精品成人片毛片_黃色A片三級三級三級无码_日本不卡高清视频v中文字幕_高清欧美视频一区二区
            1. <source id="nejs2"></source>
            2. <video id="nejs2"></video>
              <source id="nejs2"></source>