How to combine linear data into a hierarchy?

Viewed 41

I am reading a document with bullets and subbullets. The data is in a linear list of dictionaries having text of each line and a tag identifying a bullet, subbullet1, subbullet2, and so on.

  • First bullet
    • first subbullet of bullet 1
  • Second bullet
    • first subbullet of bullet 2
      • first subbullet of subbullet 1 of bullet 2

I want to convert the above data into the following format:

tags: [
    {
        tag: bulletlist
        bullets: [
            {
                text: First bullet
                level: 1
                subbullets: [
                    {
                        text: first subbullet of bullet 1,
                        level: 2
                        subbullets: []
                    }
                ]
            },
            {
                text: Second bullet,
                level: 1
                subbullets: [
                    {
                        text: first subbullet of bullet 2,
                        level: 2
                        subbullets: [
                            {
                                text: first subbullet of subbullet 1 of bullet 2
                                level: 3
                                subbullets: []
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

I have made the below class:

class TreeNode:

    def __init__(self, number, name, subullet):
        self.number = number
        self.text = name
        self.subullet=subullet
        self.children = []
    
    def addChild(self, child):
        self.children.append(child)
    
    def serialize(self):
        s={}
        for child in self.children:
            
            s["text"] = self.text
            s["level"] = self.number
            s[child.subullet] = child.serialize()
            
        return s

root=TreeNode("","root","bullets")

element1=TreeNode("1","First bullet","bullet1")
element2=TreeNode("2","Second bullet","bullet2")

root.addChild(element1)
root.addChild(element2)


child1_element1=TreeNode("1.1","first subbullet of bullet 1","bullet 1.1")
child2_element2=TreeNode("1.2","first subbullet of bullet 2","bullet 2.1")
child1_child2=TreeNode("1.2","first subbullet of subbullet 1 of bullet 2","bullet 2.1.1.")


element1.addChild(child1_element1)
element2.addChild(child2_element2)
child2_element2.addChild(child1_child2)

end=TreeNode(None,None,None)
child1_element1.addChild(end)
child1_child2.addChild(end)

print(json.dumps(root.serialize(), indent=4)) 

Output is:
{
    "text": "root",
    "level": "",
    "bullet1": {
        "text": "First bullet",
        "level": "1",
        "bullet 1.1": {
            "text": "first subbullet of bullet 1",
            "level": "1.1",
            "null": {}
        }
    },
    "bullet2": {
        "text": "Second bullet",
        "level": "2",
        "bullet 2.1": {
            "text": "first subbullet of bullet 2",
            "level": "1.2",
            "bullet 2.1.1.": {
                "text": "first subbullet of subbullet 1 of bullet 2",
                "level": "1.2",
                "null": {}
            }
        }
    }
}

It gives me the below format but I want the format shown above which is nested list of dictionaries:

0 Answers
Related