我有一个数组(树),例如:
['Humidity\n', ['High', ['Outlook\n', ['Sunny', ['Temp\n', ['Hot', ['Wind\n', ['Weak', ['No']], ['Strong', ['No']]]], ['Mild', ['No']]]], ['Overcast', ['Temp\n', ['Hot', ['Yes']], ['Mild', ['Yes']]]], ['Rain', ['Temp\n', ['Mild', ['Wind\n', ['Weak', ['Yes']], ['Strong', ['No']]]]]]]], ['Normal', ['Wind\n', ['Weak', ['Outlook\n', ['Rain', ['Yes']], ['Overcast', ['Temp\n', ['Cool', ['Yes']], ['Hot', ['Yes']]]], ['Sunny', ['Yes']]]], ['Strong', ['Temp\n', ['Cool', ['No']], ['Mild', ['Outlook\n', ['Rain', ['Yes']], ['Sunny', ['Yes']]]]]]]]]
我想把它打印成一棵树。这是我的尝试
def printTree(treeToPrint, tab):
print tab + treeToPrint[0]
newTab = tab + "--"
nextTree = treeToPrint[1:]
for tree in nextTree:
print newTab + tree[0]
printTree(tree[1], newTab + "--")
它正在打印输出,例如:
Humidity
--High
----Outlook
------Sunny
--------Temp
----------Hot
------------Wind
--------------Weak
----------------No
--------------Strong
----------------No
----------Mild
------------No
------Overcast
--------Temp
----------Hot
------------Yes
----------Mild
------------Yes
------Rain
--------Temp
----------Mild
------------Wind
--------------Weak
----------------Yes
--------------Strong
----------------No
--Normal
----Wind
------Weak
--------Outlook
----------Rain
------------Yes
----------Overcast
------------Temp
--------------Cool
----------------Yes
--------------Hot
----------------Yes
----------Sunny
------------Yes
------Strong
--------Temp
----------Cool
------------No
----------Mild
------------Outlook
--------------Rain
----------------Yes
--------------Sunny
----------------Yes
有人可以帮我以漂亮的格式表示这棵树。