<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC=1,5] >[success] # defaultdict 解決keyError 問題 ~~~ 1.使用傳統的字典的時候,當調用字典中不存在的key時候會出現異常錯誤 2.解決的辦法我們是有get(k,None) 第一個是鍵第二個是默認值 3.調用的時候我們可以使用get,創建的時候,我們就可以使用defaultdict方法 ~~~ >[danger] ##### 傳統字典類型 -- keyError 異常 * 常見問題 ,統計列表數據 ~~~ bag = ['orange', 'cherry', 'apple', 'blueberry'] count = {} for fruit in bag: count[fruit] += 1 打印結果: KeyError: 'orange' ~~~ >[danger] ##### 案例--defaultdict,統計列表中元素產生的次數 ~~~ 1.可以在字典初始化的時候,為不存的k 設置好默認值,但必須是可調用的對象, 字典,元組,列表,甚至lambda/函數 ~~~ * 上面的例子就可以寫成 ~~~ from collections import defaultdict bag = ['orange', 'cherry', 'apple', 'blueberry'] dd = defaultdict(lambda:0) for i in bag: dd[i] +=1 打印結果: defaultdict(<function <lambda> at 0x000000DFB645A7B8>, {'orange': 1, 'cherry': 1, 'apple': 1, 'blueberry': 1}) ~~~ * 下面的例子是用傳統的方法,做一次K是否存在判斷,不存在設置默認值 ~~~ bag = ['orange', 'cherry', 'apple', 'blueberry'] dd = {} for i in bag: if i not in dd: dd[i] = 1 else: dd[i] += 1 print(dd) 打印結果: {'orange': 1, 'apple': 1, 'cherry': 1, 'blueberry': 1} ~~~ >[danger] ##### 基本的使用 * 搭配 常見的 對象 list set ~~~ from collections import defaultdict dd = defaultdict(list) print(dd['k']) 打印結果: [] ~~~ * 自帶的函數例如int ~~~ dd = defaultdict(int) print(dd['k']) 打印結果: 0 ~~~ * 搭配 匿名函數 ~~~ from collections import defaultdict dd = defaultdict(lambda :0) print(dd['k']) 打印結果: 0 ~~~ * 搭配 函數 ~~~ from collections import defaultdict def zero(): return 0 dd = defaultdict(zero) print(dd['k']) 打印結果: 0 ~~~ >[danger] ##### 原理實現 ~~~ from collections import defaultdict print(defaultdict.__missing__.__doc__) 打印結果: __missing__(key) # Called by __getitem__ for missing key; pseudo-code: if self.default_factory is None: raise KeyError((key,)) self[key] = value = self.default_factory() return value ~~~ <a href="https://docs.python.org/3/library/stdtypes.html#dict"> 官方文檔對__missing__ 解釋</a> 如下 ~~~ Return the item of d with key key. Raises a KeyError if key is not in the map. If a subclass of dict defines a method __missing__() and key is not present, the d[key] operation calls that method with the key key as argument. The d[key] operation then returns or raises whatever is returned or raised by the __missing__(key) call. No other operations or methods invoke __missing__(). If __missing__() is not defined, KeyError is raised. __missing__() must be a method; it cannot be an instance variable: ~~~ ~~~ >>> class Counter(dict): ... def __missing__(self, key): ... return 0 >>> c = Counter() >>> c['red'] 0 >>> c['red'] += 1 >>> c['red'] 1 ~~~ 當訪問不存在的鍵時,dict[key]會調用__missing__()方法取得默認值。
                  <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>

                              哎呀哎呀视频在线观看