What is an efficient way to define and move a tree in python?

I have some products that need to be assigned categories. I went through some nested dictionary methods, however I just want to know an effective way to use the tree structure.

Examples of categories correspond to the following image, the indicated depth is also the maximum depth of the tree.

enter image description here

I want to take the name of the elements in the tree, look for it in a string, use it as a substring and store it in the temp list if it exists.

For example: I will search for MUV, SUV, Sedan words in a string, if SUV exists, I will keep updating temp = [Car, SUV]. After that, walk the same way as the SUV node to the end of the tree. So the list at the end will look like this [Car, SUV, Window, XYZ]

, 4 .

, 30000 .

+4
1

ete2 python package, Newick (. wiki: Newick, )

from ete2 import Tree

t = Tree("(A,(B,(E,D)));" ) # Loads a tree structure from a newick string. The returned variable ’t’ is the root node for the tree.

print t

   /-A
--|
  |   /-B
   \-|
     |   /-E
      \-|
         \-D

( )

for node in t.traverse("postorder"):
  # Do some analysis on node
  print node.name

, Python script

?

+3