Question

I am trying to generate list of objects using list comprehension in IronPython to pass it as an array to a function. The code below generates an array of TreeNode objects, for e.g. [treeNode1,treeNode2,treeNode3] I want to add a root node in the array as well such that it extends the array like [treeNode1,treeNode2,treeNode3,root] and not like [[treeNode1,treeNode2,treeNode3],root]

def _to_tree_nodes(self,lst):
        l=dict()
        for tuple in lst:
            if tuple[1] not in l:
                l.update({tuple[1]:[tuple[0]]})
            else:
                l[tuple[1]].append(tuple[0])

        root = TreeNode("Level " + str(max(l)), Array[TreeNode]([TreeNode(x) for x in l[max(l)]]))
        for i in xrange(max(l)-1,0,-1):
            root = TreeNode("Level " + str(i), Array[TreeNode]([TreeNode(x) for x in l[i]]))

        return root

How can I achieve it?

Was it helpful?

Solution

Just concatenate an extra list object:

root = TreeNode("Level " + str(i), Array[TreeNode](
    [TreeNode(x) for x in l[i]] + [root]))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top