小编Rav*_*i K的帖子

Python 3.6-以可读的树结构打印字典数据

如何使用 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)

python dictionary

1
推荐指数
1
解决办法
9108
查看次数

标签 统计

dictionary ×1

python ×1