创建层次结构树,节点名称为str

时间:2018-06-19 09:59:55

标签: python class tree xlsxwriter

我需要创建一个树,显示每个级别的公式计算。我以字典形式接收数据

level1={'Operating Cash Flow':['Total Revenue','Operating Expenses']}   
level2={'Total Revenue':['Net Income','Change in Asset'],'Operating Expenses':['Non Cash Expense','XYZ']}  

等等。想法是使用公开数据创建复杂的跟踪器。然后以分层格式输出(在excel中)以显示它的计算。像这样的东西

+-------------------------------------------------------+
|                       Operating                       |
|                        Cash Flow                      |
+-------------------------------------------------------+
|                           60                          |
+-------------------------------------------------------+
|         Total Revenue        |    Operating Expense   |
+------------------------------+------------------------+
|              10              |           50           |
+------------------------------+------------------------+
| Net Income | Change in asset | Non cash Expense | XYZ |
+------------+-----------------+------------------+-----+
| 20         | -10             | 40               | 10  |
+------------+-----------------+------------------+-----+

所以我试图为此制作一个类似树的结构。

class Node(object):
    def __init__(self):
        self.score=0
        self.fields=[]
        self.weights=1
        self.children=[]

    def add_child(self,obj):
        self.children.append(obj)
        self.weights= self.weights + obj.weights -1

    def enter_score(self,value):
        self.score=value

Node.fields将存储其字段,Node.children指向该值。由于dict中的字段名是str,我不知道如何制作这个完整的树。此外,任何有关如何在树完成后将其输出为excel的帮助都会有所帮助。

0 个答案:

没有答案