Home > Software design >  I want to take an element from a list and put it in a new list
I want to take an element from a list and put it in a new list

Time:12-29

lines = [[0,1],[0,2],[0,3],[1,4],[2,5],[2,6],[3,7],[3,8],[3,9]], where node is a list of Each of them contains two numbers, but the left one is the parent node and the right one is the child node. The parent node is fixed at 0. The lines list shows that 0 (parent node) is connected to 1, 2, and 3 (child nodes), 1 to 4, 2 to 5 and 6, and 3 to 7, 8, and 9. We want to connect the numbers 0→1→4, 0→2→5, 0→2→6, 0→3→7, 0→3→8, 0→3→9, and finally [0, [1, [4]], [2, [5], [6]], [3, [7], [8], [9]].

In the current code, printing the result would result in [[0], [1, [4]], [2, [5, 6]], [3, [7, 8, 9]]], possibly because node.append(j[1]) has [5, 6], [7, 8, 9] in the node list as they are listed. Is there any way to make this print as [0, [1, [4]], [2, [5], [6]], [3, [7], [8], [9]]]?

lines = [[0,1],[0,2],[0,3],[1,4],[2,5],[2,6],[3,7],[3,8],[3,9]]

result = []
node_n = []
for i in lines:
    node = list()
    for j in lines:
        if i[0] == j[0]:
            node.append(j[1])
            # print(node)
    node_list = [i[0],node]    
    if node_list not in result:
        result.append(node_list)
result.sort()
del result[0][1]
print(result)

CodePudding user response:

def nodes(n):
    return [n] [nodes(y) for [x,y] in lines if x==n]
nodes(0)
# → [0, [1, [4]], [2, [5], [6]], [3, [7], [8], [9]]]

It returns a list made of node n, and of all the list representing subtrees for the successors of n (obtained recursively)

So, for nodes(0) it returns [0] [nodes(1), nodes(2), nodes(3)]

And nodes(1) is [1] [nodes(4)]

And nodes(4) is just [4]

Etc.

CodePudding user response:

You can just correct the code as follows:

lines = [[0,1],[0,2],[0,3],[1,4],[2,5],[2,6],[3,7],[3,8],[3,9]]

result = []
node_n = []
for i in lines:
    node = []
    for j in lines:
        if i[0] == j[0]:
            node.append(j[1])
    node_list = [i[0],node]    
    if node_list not in result:
        result.append(node_list)
result.sort()

# build the tree
tree = [result[0][0]]
for i in result[1:]:
    children = []
    for j in i[1]:
        children.append([j])
    tree.append([i[0]]   children)

print(tree)

Hope it works for you.

CodePudding user response:

You could use a graph traversal for this output. Use networkx to build a graph from the lines list and then you need to recursively traverse (in a depth first fashion) to build the list. There is no ready method (api) available in networkx to get the desired output. One way is below:

import networkx as nx
G = nx.Graph()
G.add_edges_from(lines)
def construct(lst, G, node, visited):
    if node in visited:
        return lst
    visited.add(node)
    lst.append(node)
    for nbr in nx.neighbors(G, node):
        if nbr not in visited:
            lst.append(construct([], G, nbr, visited))
    return lst
print(construct([], G, 0, set()))

[0, [1, [4]], [2, [5], [6]], [3, [7], [8], [9]]]
  • Related