[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__()方法取得默认值。