我尝试使用 Trie 数据结构来解决一些编码问题。对于 trie 中的每个节点,您通常会放置其子节点的引用列表。因此,如果查找中不存在某些子节点,我考虑使用 defaultdict 创建默认的空 trie 节点。但是,我不知道如何使用 defaultdict 来引用包含它的类。
我尝试了两种方法,都失败了。以下是我尝试过的。
from dataclasses import dataclass
from collections import defaultdict
@dataclass
class TrieNode():
is_word = False
children = defaultdict("TrieNode")
Run Code Online (Sandbox Code Playgroud)
上面的代码产生
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "<stdin>", line 4, in TrieNode
TypeError: first argument must be callable or None
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "<stdin>", line 4, in TrieNode
TypeError: first argument must be callable or None …Run Code Online (Sandbox Code Playgroud) 众所周知,Storm拓扑可以有多个Spout/Bolts.当我们发布Storm拓扑时,我们必须定义spouts和bolt之间的依赖关系.我想知道在拓扑运行时我可以注册新的螺栓吗?