我遇到过这个创建树的python技巧:
def Tree():
return defaultdict(Tree)
t = Tree()
t["key1"]["key2"]["key3"] = value
所以我使用它的方式是,无论我处于哪个维度,此数据结构中的每个键都是唯一的。但是,这会带来一些问题,因为我希望能够根据特定父级插入新密钥,例如我想将一个子项插入key3
,如何以一种方式遍历它找到key3
?例如,我可以采取哪些策略/方法来找到给定的“密钥”?
我有一种感觉,这可以递归地解决,但我对递归和编程相当缺乏经验,这是我第一次尝试解决一组群体类型问题。谢谢!
答案 0 :(得分:2)
这是一个递归搜索"路径"的函数。到节点 - 即定位所需节点的父节点列表:
def findNode(tree, findKey, nodeType):
if type(tree) != nodeType: # Check if this is another node or a leaf
# Reached the end of a branch, and the key was not found, so:
# return None in order to trigger a `TypeError` and reject this branch
return None
for key in tree:
if key == findKey:
# Key was found - return the final part of the path
return [key]
else:
try:
# Search the next level in this branch
return [key] + findNode(tree[key], findKey, nodeType)
except TypeError:
pass
from collections import defaultdict
def Tree():
return defaultdict(Tree)
t = Tree()
t[1][2][3][4] = 99
t[1][2][7][9] = 101
t[1][10][19][22] = 44
t[1][10][19][77] = 2822
findNode(t, 22, type(t))
# Result:
# [1, 10, 19, 22]
findNode(t, 7, type(t))
# Result:
# [1, 2, 7]
答案 1 :(得分:2)
我使用这个简单的递归函数选择find
相关节点:
def find(t, search):
#
# Find the *first* dictionary object having
# the key "search"
for k in t:
if k == search:
return t
if isinstance(t[k],dict):
result = find(t[k],search)
if result:
return result
return None
获得节点后,您可以根据需要进行更改:
>>> from pprint import pprint
>>> pprint(t)
defaultdict(<function Tree at 0x1c17488>, {
'key1': defaultdict(<function Tree at 0x1c17488>, {
'key2': defaultdict(<function Tree at 0x1c17488>, {
'key3': 99})})})
>>> node = find(t, "key3")
>>> pprint(node)
defaultdict(<function Tree at 0x1c17488>, {
'key3': 99})
由于您现在有一个引用到新发现的字典,通过该引用更改它将改变&#34;原始&#34;树 - 因为两者都包含对同一对象的引用。我不太清楚,所以看看这个例子:
>>> node["key3b"]=0
>>> pprint(t)
defaultdict(<function Tree at 0x1c17488>, {
'key1': defaultdict(<function Tree at 0x1c17488>, {
'key2': defaultdict(<function Tree at 0x1c17488>, {
'key3': 99,
'key3b': 0})})})