def find_depth(tree, target, depth=0):
for key, subtree in tree.items():
if key == target:
return depth
d = find_depth(subtree, target, depth + 1)
if d is not None:
return d
return None
mall = {
"전체": {
"의류": {
"상의": {},
"하의": {
"청바지": {},
"치마": {}
}
},
"전자기기": {
"노트북": {},
"스마트폰": {}
}
}
}
target = "청바지"
depth = find_depth(mall, target)
print(depth)
To embed this project on your website, copy the following code and paste it into your website's HTML: