<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Python `len()`函數 > 原文: [https://thepythonguru.com/python-builtin-functions/len/](https://thepythonguru.com/python-builtin-functions/len/) * * * 于 2020 年 1 月 7 日更新 * * * `len()`函數計算對象中的項目數。 其語法如下: ```py len(obj) -> length ``` | 參數 | 描述 | | --- | --- | | `obj` | `obj`可以是字符串,列表,字典,元組等。 | 這是一個例子: ```py >>> >>> len([1, 2, 3, 4, 5]) # length of list 5 >>> >>> print(len({"spande", "club", "diamond", "heart"})) # length of set 4 >>> >>> print(len(("alpha", "beta", "gamma"))) # length of tuple 3 >>> >>> print(len({ "mango": 10, "apple": 40, "plum": 16 })) # length of dictionary 3 ``` 試試看: ```py # length of list print(len([1, 2, 3, 4, 5])) # length of set print(len({"spande", "club", "diamond", "heart"})) # length of tuple print(len(("alpha", "beta", "gamma"))) # length of dictionary print(len({ "mango": 10, "apple": 40, "plum": 16 })) ``` 具有諷刺意味的是,`len()`函數不適用于生成器。 嘗試在生成器對象上調用`len()`將導致`TypeError`異常。 ```py >>> >>> def gen_func(): ... for i in range(5): ... yield i ... >>> >>> >>> len(gen_func()) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: object of type 'generator' has no len() >>> ``` 試一試: ```py def gen_func(): for i in range(5): yield i print(len(gen_func())) ``` ## `len()`與用戶定義的對象 * * * 要在用戶定義的對象上使用`len()`,您將必須實現`__len__()`方法。 ```py >>> >>> class Stack: ... ... def __init__(self): ... self._stack = [] ... ... def push(self, item): ... self._stack.append(item) ... ... def pop(self): ... self._stack.pop() ... ... def __len__(self): ... return len(self._stack) ... >>> >>> s = Stack() >>> >>> len(s) 0 >>> >>> s.push(2) >>> s.push(5) >>> s.push(9) >>> s.push(12) >>> >>> len(s) 4 >>> ``` 試一試: ```py class Stack: def __init__(self): self._stack = [] def push(self, item): self._stack.append(item) def pop(self): self._stack.pop() def __len__(self): return len(self._stack) s = Stack() print(len(s)) s.push(2) s.push(5) s.push(9) s.push(12) print(len(s)) ``` * * * * * *
                  <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>

                              哎呀哎呀视频在线观看