<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### 問題: 如果一個可迭代對象的元素個數超過變量個數時,會拋出一個 ValueError。 那么怎樣才能從這個可迭代對象中解壓出 N個元素出來? ***** ### 解決方案: Python 的星號表達式可以用來解決這個問題,比如,你在學習一門課程,在學期末的時候 你想統計下家庭作業的平級成績,但是排除掉第一個和最后一個分數。如果只有四個分數, 你可能就直接去簡單的手動賦值,但如果有 24 個呢?這時候星號表達式就派上用場了。 ***** 例子: ```python grades = [60, 40, 70, 80, 65, 49, 12, 34, 99, 75, 46, 71, 68, 22, 81, 98, 75, 65, 15, 75] def avg(middle): print(middle) def drop_fist_last(grades): print(sorted(grades)) first, *middle, last = sorted(grades) return avg(middle) drop_fist_last(grades) ``` 輸出: ``` [12, 15, 22, 34, 40, 46, 49, 60, 65, 65, 68, 70, 71, 75, 75, 75, 80, 81, 98, 99] [15, 22, 34, 40, 46, 49, 60, 65, 65, 68, 70, 71, 75, 75, 75, 80, 81, 98] ``` ### 另外一種情況,假設你現在有一些用戶的記錄列表,每條記錄包含一個名字,郵件,接著就是不確定數量的電話號碼。你可以像下面這樣分解這些記錄 ```Python record = ('Dave', 'dave@example.com', '773-555-1212', '847-555-1212') name, email, *phone_numbers = record print(name) # Dave print(email) # dave@example.com print(phone_numbers) # ['773-555-1212', '847-555-1212'] ``` ### 值得注意的是上面解壓出來的 `phone_numbers` 變量永遠是列表類型,不管解壓的電話號碼數量是多少(包括0個)。所以,任何使用到 `phone_numbers` 變量的代碼就不需要做多余的類型檢查去確認它是否是列表類型了。 **星號表達式也能用在列表的開始部分** ```Python *trailing, current = [10, 8, 7, 1, 9, 5, 10, 3] print(trailing) # [10, 8, 7, 1, 9, 5, 10] print(current) # 3 ``` ### 擴展的迭代解壓語法是專門為解壓不確定個數或任意個數元素的可迭代對象而設計的。通常,這些可迭代對象的元素結構有確定的規則(比如第一個元素后面都是電話號碼),星號表達式讓開發人員可以很容易的利用這些規則來解壓出元素來。而不是通過一些比較復雜的手段去獲取這些關聯的元素值。 ** 星號表達式在迭代元素為可變長元組的序列時是很有用的。比如,下面是一個帶有標簽的元組序列** ```Python records = [ ('foo', 1, 2), ('bar', 'hello'), ('foo', 3, 4) ] def do_foo(x,y): print('foo',x,y) def do_bar(s): print('bar',s) for tag, *args in records: if tag == 'foo': do_foo(*args) else: do_bar(*args) ``` 輸出: ``` foo 1 2 bar hello foo 3 4 ``` ** 星號解壓語法在字符串操作的時候也會很有用。比如字符串的分割。** ```Python line = 'nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false' uname, *fields, homedir, sh = line.split(':') print(uname) # nobody print(fields) # ['*', '-2', '-2', 'Unprivileged User'] print(homedir) # /var/empty print(sh) # /usr/bin/false ``` ***** **有時候,你想解壓一些元素后丟棄它們,你不能簡單就使用 * ,但是你可以使用一個普通的廢棄名稱,比如 _ 或者 ign (ignore)** ```Python record = ('ACME', 50, 123.45, (12, 18, 2018)) name, *_, (*_, year) = record print(name) # ACME print(year) # 2018 ``` ***** **在很多函數式語言中,星號解壓語法跟列表處理有很多相似之處。比如,如果你有一個列表,你可以很容易的將它分割成前后兩部分** ```Python items = [1, 10, 7, 4, 5, 9] head, *tail, last = items print(head) # 1 print(last) # 9 ``` ***** **還能用這種分割語法去巧妙的實現遞歸算法。** ```Python items = [1, 10, 7, 4, 5, 9] def sums(items): head, *tail = items return head + sums(tail) if tail else head ``` 原文: > [http://python3-cookbook.readthedocs.io/zh\_CN/latest/c01/p02\_unpack\_elements\_from\_iterables.html](http://python3-cookbook.readthedocs.io/zh_CN/latest/c01/p02_unpack_elements_from_iterables.html)
                  <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>

                              哎呀哎呀视频在线观看