<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Python `min()`函數 > 原文: [https://thepythonguru.com/python-builtin-functions/min/](https://thepythonguru.com/python-builtin-functions/min/) * * * 于 2020 年 1 月 7 日更新 * * * `min()`函數返回最小的輸入值。 其語法如下: ```py min(iterable[, default=obj, key=func]) -> value ``` | 參數 | 描述 | | --- | --- | | `iterable`(必填) | 可迭代對象,例如字符串,列表,元組等。 | | `default`(可選) | 如果可迭代對象為空,則返回默認值。 | | `key`(可選) | 它引用單個參數函數以自定義排序順序。 該函數應用于迭代器上的每個項目。 | 要么 ```py min(a, b, c, ...[, key=func]) -> value ``` | 參數 | 描述 | | --- | --- | | `a, b, c ...` | 比較項目 | | `key`(可選) | 它引用單個參數函數以自定義排序順序。 該函數適用??于每個項目。 | 如果以可迭代方式調用`min()`,它將返回其中的最小項。 如果可迭代對象為空,則返回`default`值(假設已提供),否則引發`ValueError`異常。 如果使用多個參數調用`min()`,它將返回最小的參數。 讓我們看一些例子: **示例 1** :以可迭代方式調用`min()` ```py >>> >>> min("abcDEF") # find smallest item in the string 'D' >>> >>> >>> min([2, -1, 4, 3]) # find smallest item in the list -1 >>> >>> >>> min(("one", "two", "three")) # find smallest item in the tuple 'one' >>> >>> >>> min({1: "one", 2: "two", 3: "three"}) # find smallest item in the dict 1 >>> >>> >>> min([]) # empty iterable causes ValueError Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: min() arg is an empty sequence >>> >>> >>> min([], default=0) # supressing the error with default value 0 >>> ``` 試試看: ```py # find smallest item in the string print(min("abcDEF")) # find smallest item in the list print(min([2, -1, 4, 3])) # find smallest item in the tuple print(min(("one", "two", "three"))) # find smallest item in the dict print(min({1: "one", 2: "two", 3: "three"})) #print(min([])) # empty iterable causes ValueError # supressing the error with default value print(min([], default=0)) ``` **示例 2** :使用多個參數調用`min()` ```py >>> >>> min(20, 10, 30, -5) -5 >>> >>> >>> min("c", "b", "a", "Y", "Z") 'Y' >>> >>> >>> min(3.14, -9.91, 2.41) -9.91 >>> ``` 試一試: ```py print(min(20, 10, 30, -5)) print(min("c", "b", "a", "Y", "Z")) print(min(3.14, -9.91, 2.41)) ``` 試圖在不同類型的對象中找到最大值會導致錯誤。 ```py >>> >>> min(10, "pypi") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: str() > int() >>> >>> >>> min(5, [-10, 55]) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: list() > int() >>> ``` ## 自定義排序順序 * * * 為了自定義排序順序,我們使用`key`命名參數。 它的工作原理類似于[`sorted()`](/python-builtin-functions/sorted/)函數的`key`命名參數。 這是一個使用鍵參數使字符串比較區分大小寫的示例。 ```py >>> >>> min("c", "b", "a", "Y", "Z") 'Y' >>> >>> min("c", "b", "a", "Y", "Z", key=str.lower) 'a' >>> ``` 試一試: ```py print(min("c", "b", "a", "Y", "Z")) print(min("c", "b", "a", "Y", "Z", key=str.lower)) ``` 以下是另一個示例,其中我們根據字符串的長度而不是其 ASCII 值比較字符串。 ```py >>> >>> min(("java", "python", "z++")) 'java' >>> >>> min(("java", "python", "z++"), key=len) 'z++' >>> ``` 試一試: ```py print(min(("java", "python", "z++"))) print(min(("java", "python", "z++"), key=len)) ``` 還存在一個互補函數,稱為[`max()`](/python-builtin-functions/max/),可找到最大的輸入值。 * * * * * *
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看