如何使用 Python 以更易读的树格式打印下面的字典数据,如下所示。这是我第一次来这里,请原谅我的无知。
TempDict =
{'outlook': {'sunny': {'humidity': {'high': 'no', 'normal': 'yes'}}, 'rainy': {'wind': {'strong': 'no', 'weak': 'yes'}}, 'overcast': 'yes'}}
Run Code Online (Sandbox Code Playgroud)
所需的输出格式 -
outlook=sunny
humidity=high:no
humidity=normal:yes
outlook=rainy
wind=strong:no
wind=weak:yes
Run Code Online (Sandbox Code Playgroud)
像这样打印的类:
class DecisionNode:
def __init__(self, attribute):
self.attribute = attribute
self.children = {}
# Visualizes the tree
def display(self, level = 0):
if self.children == {}:
# reached leaf level
print(": ", self.attribute, end="")
else:
for value in self.children.keys():
prefix = "\n" + " " * level * 4
print(prefix, self.attribute, "=", value, …Run Code Online (Sandbox Code Playgroud)