python - d.get(key) 与 d[key] 的 Python defaultdict 有什么区别?
问题描述
我正在使用python defaultdict,我注意到了这一点:
from collections import defautdict
class TrieNode(object):
def __init__(self):
self.children = defaultdict(TrieNode)
self.is_word = False
temp = TrieNode()
如果我做:
temp = temp.children['A']
temp
将是一个新TrieNode
实例
但如果我这样做:
temp = temp.children.get('B')
temp
可以是无。
为什么d.get(key)
vsd[key]
在 defaultdict 中表现不同?
解决方案
推荐阅读
- regex - 正则表达式匹配 sed 中的非 ASCII 字符
- pandas - Pandas group by 从旧列计算新列
- python - 将python字典列表转换为树格式
- html - 如何在 :after 伪元素中添加动画 gif
- python - 如何制作基于命令行的库,例如 pip 或 conda
- c# - Dynamics Crm 插件中的 Interlocked.Increment?
- excel - 使用 VBA 打开单元格的双下划线
- javascript - 如何仅从 mySQL 获取值,以便查询结果不在表内?
- javascript - 使用 jquery 将 div 内容剪切到表体
- hive - 为什么删除功能在 hive shell 中不起作用?