在读取dict
的key
和value
时,如果key
不存在,就会触发KeyError
错误,如:
t = { 'a': '1', 'b': '2', 'c': '3', } print(t['d'])
就会出现:
<code class="language-plain" style="font-family:Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;font-size:13.6px;background:transparent;word-spacing:normal;line-height:inherit;border:0px;display:inline;">KeyError: 'd'
<span class="line-numbers-rows" style="font-size:13.6px;letter-spacing:-1px;border-right:1px solid rgb(153,153,153);"><span style="display:block;"></span></span></code>
第一种解决方法
首先测试key是否存在,然后才进行下一步操作,如:
t = { 'a': '1', 'b': '2', 'c': '3', } if 'd' in t: print(t['d']) else: print('not exist')
会出现:
<code class="language-plain" style="font-family:Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;font-size:13.6px;background:transparent;word-spacing:normal;line-height:inherit;border:0px;display:inline;">not exist
<span class="line-numbers-rows" style="font-size:13.6px;letter-spacing:-1px;border-right:1px solid rgb(153,153,153);"><span style="display:block;"></span></span></code>
第二种解决方法
利用dict
内置的get(key[,default])
方法,如果key
存在,则返回其value
,否则返回default
;使用这个方法永远不会触发KeyError
,如:
t = { 'a': '1', 'b': '2', 'c': '3', } print(t.get('d'))
会出现:
<code class="language-plain" style="font-family:Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;font-size:13.6px;background:transparent;word-spacing:normal;line-height:inherit;border:0px;display:inline;">None
<span class="line-numbers-rows" style="font-size:13.6px;letter-spacing:-1px;border-right:1px solid rgb(153,153,153);"><span style="display:block;"></span></span></code>
加上default
参数:
t = { 'a': '1', 'b': '2', 'c': '3', } print(t.get('d', 'not exist')) print(t)
会出现:
<code class="language-plain" style="font-family:Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;font-size:13.6px;background:transparent;word-spacing:normal;line-height:inherit;border:0px;display:inline;">not exist
{'a': '1', 'c': '3', 'b': '2'}
<span class="line-numbers-rows" style="font-size:13.6px;letter-spacing:-1px;border-right:1px solid rgb(153,153,153);"><span style="display:block;"></span><span style="display:block;"></span></span></code>
第三种解决方法
利用dict
内置的setdefault(key[,default])
方法,如果key
存在,则返回其value
;否则插入此key
,其value
为default
,并返回default
;使用这个方法也永远不会触发KeyError
,如:
t = { 'a': '1', 'b': '2', 'c': '3', } print(t.setdefault('d')) print(t)
会出现:
<code class="language-plain" style="font-family:Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;font-size:13.6px;background:transparent;word-spacing:normal;line-height:inherit;border:0px;display:inline;">None
{'b': '2', 'd': None, 'a': '1', 'c': '3'}
<span class="line-numbers-rows" style="font-size:13.6px;letter-spacing:-1px;border-right:1px solid rgb(153,153,153);"><span style="display:block;"></span><span style="display:block;"></span></span></code>
加上default
参数:
t = { 'a': '1', 'b': '2', 'c': '3', } print(t.setdefault('d', 'not exist')) print(t)
会出现:
<code class="language-plain" style="font-family:Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;font-size:13.6px;background:transparent;word-spacing:normal;line-height:inherit;border:0px;display:inline;">not exist
{'c': '3', 'd': 'not exist', 'a': '1', 'b': '2'}
<span class="line-numbers-rows" style="font-size:13.6px;letter-spacing:-1px;border-right:1px solid rgb(153,153,153);"><span style="display:block;"></span><span style="display:block;"></span></span></code>
第四种解决方法
向类dict
增加__missing__()
方法,当key
不存在时,会转向__missing__()
方法处理,而不触发KeyError
,如:
t = { 'a': '1', 'b': '2', 'c': '3', } class Counter(dict): def __missing__(self, key): return None c = Counter(t) print(c['d'])
会出现:
<code class="language-plain" style="font-family:Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;font-size:13.6px;background:transparent;word-spacing:normal;line-height:inherit;border:0px;display:inline;">None
<span class="line-numbers-rows" style="font-size:13.6px;letter-spacing:-1px;border-right:1px solid rgb(153,153,153);"><span style="display:block;"></span></span></code>
更改return
值:
t = { 'a': '1', 'b': '2', 'c': '3', } class Counter(dict): def __missing__(self, key): return key c = Counter(t) print(c['d']) print(c)
会出现:
<code class="language-plain" style="font-family:Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;font-size:13.6px;background:transparent;word-spacing:normal;line-height:inherit;border:0px;display:inline;">d
{'c': '3', 'a': '1', 'b': '2'}
<span class="line-numbers-rows" style="font-size:13.6px;letter-spacing:-1px;border-right:1px solid rgb(153,153,153);"><span style="display:block;"></span><span style="display:block;"></span></span></code>
第五种解决方法
利用collections.defaultdict([default_factory[,...]])
对象,实际上这个是继承自dict
,而且实际也是用到的__missing__()
方法,其default_factory
参数就是向__missing__()
方法传递的,不过使用起来更加顺手:
如果default_factory
为None
,则与dict
无区别,会触发KeyError
错误,如:
import collections t = { 'a': '1', 'b': '2', 'c': '3', } t = collections.defaultdict(None, t) print(t['d'])
会出现:
<code class="language-plain" style="font-family:Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;font-size:13.6px;background:transparent;word-spacing:normal;line-height:inherit;border:0px;display:inline;">KeyError: 'd'
<span class="line-numbers-rows" style="font-size:13.6px;letter-spacing:-1px;border-right:1px solid rgb(153,153,153);"><span style="display:block;"></span></span></code>
但如果真的想返回None
也不是没有办法:
import collections t = { 'a': '1', 'b': '2', 'c': '3', } def handle(): return None t = collections.defaultdict(handle, t) print(t['d'])
会出现:
<code class="language-plain" style="font-family:Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;font-size:13.6px;background:transparent;word-spacing:normal;line-height:inherit;border:0px;display:inline;">None
<span class="line-numbers-rows" style="font-size:13.6px;letter-spacing:-1px;border-right:1px solid rgb(153,153,153);"><span style="display:block;"></span></span></code>
如果default_factory
参数是某种数据类型,则会返回其默认值,如:
import collections t = { 'a': '1', 'b': '2', 'c': '3', } t = collections.defaultdict(int, t) print(t['d'])
会出现:
<code class="language-plain" style="font-family:Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;font-size:13.6px;background:transparent;word-spacing:normal;line-height:inherit;border:0px;display:inline;">0
<span class="line-numbers-rows" style="font-size:13.6px;letter-spacing:-1px;border-right:1px solid rgb(153,153,153);"><span style="display:block;"></span></span></code>
又如:
import collections t = { 'a': '1', 'b': '2', 'c': '3', } t = collections.defaultdict(list, t) print(t['d'])
会出现:
<code class="language-plain" style="font-family:Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;font-size:13.6px;background:transparent;word-spacing:normal;line-height:inherit;border:0px;display:inline;">[]
<span class="line-numbers-rows" style="font-size:13.6px;letter-spacing:-1px;border-right:1px solid rgb(153,153,153);"><span style="display:block;"></span></span></code>
注意:
如果dict
内又含有dict
,key
嵌套获取value
时,如果中间某个key
不存在,则上述方法均失效,一定会触发KeyError
:
import collections t = { 'a': '1', 'b': '2', 'c': '3', } t = collections.defaultdict(dict, t) print(t['d']['y'])
实际操作:
for rb in data:
rb.setdefault('telephone') #当没有telephone时,设置为None
以上内容参考:https://blog.csdn.net/chenbindsg/article/details/73864045