by abcxjeb284 · 5y ago · 0 comments
state2name2visited = defaultdict(lambda: defaultdict(list))
state2name2visited[“PA”][“Joe].append(“Pittsburgh”) def tree():
return defaultdict(tree)
>>> t = tree()
>>> t['a']['b']['c'] = 10
>>> t
defaultdict(<function tree at 0x10c40df28>, {'a':
defaultdict(<function tree at 0x10c40df28>, {'b':
defaultdict(<function tree at 0x10c40df28>, {'c': 10})})}) $M{hello}[4]{world}[0]++
gives you: {
'hello' => [
undef,
undef,
undef,
undef,
{
'world' => [
1
]
}
]
} trie_struct: Callable = lambda: defaultdict(trie_struct)
trie = trie_struct()
for word in words:
ref = trie
for char in word:
ref = ref[char]
It won't work with words that substring other words, but its interesting. >>> state2name2visited = {}
>>> state2name2visited.setdefault("PA", {}).setdefault("Joe", []).append("Pittsburgh")
>>> state2name2visited
{'PA': {'Joe': ['Pittsburgh']}}Also setdefault causes confusion for less experienced users in a way that the defaultdict format does not.