Last active
September 8, 2019 13:26
-
-
Save chamoda/2e017717293d41a4762a812f2a0072d5 to your computer and use it in GitHub Desktop.
Convert tabular data to a forest.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# First row is child, second row is parent. | |
rows = [ | |
('h', 'd'), | |
('f', 'c'), | |
('e', 'b'), | |
('b', 'a'), | |
('d', 'b'), | |
('c', 'a'), | |
('g', 'c'), | |
('i', 'd'), | |
('j', 'd'), | |
] | |
# Posible roots are intersection of parents and children | |
children = set(map(lambda row: row[0], rows)) | |
parents = set(map(lambda row: row[1], rows)) | |
roots = parents - children | |
for root in roots: | |
rows.append((root, root)) | |
# Create nodes dictionary | |
nodes = {} | |
for i in rows: | |
id, parent_id = i | |
nodes[id] = { 'id': id } | |
# Create trees and parent-child relations | |
forest = [] | |
for i in rows: | |
id, parent_id = i | |
node = nodes[id] | |
# Either make the node a new tree or link it to its parent | |
if id == parent_id: | |
# Start a new tree in the forest | |
forest.append(node) | |
else: | |
# Add new_node as child to parent | |
parent = nodes[parent_id] | |
if not 'children' in parent: | |
# Ensure parent has a 'children' field | |
parent['children'] = [] | |
children = parent['children'] | |
children.append(node) | |
print(forest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment