私は次のような配列(ツリー)を持っています:
['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
誰かが私がこの木をきれいな形式で表現するのを手伝ってくれませんか。