I've got a set of data like this:
data = { 1: {"root": [2],
"leaf": [10, 11, 12],
},
2: {"root": [1,3],
"leaf": [13, 14, 15],
},
3: { "root": [2],
"leaf": [16, 17],
},
4: {"root": [],
"leaf": [17, 18, 19],
},
5: { "root": [],
"leaf": [20, 21]
},
}
From this data, the initial key is a root node index, it contains a dictionary explaining which root nodes and leaf nodes are related to it.
I want to merge all indexes into related lists.
- A root index connected by a root index, both/all root indexes and all leaf indexes are merged in the resulting list.
- A root index may be connected to another root via a leaf, root indexes and all leaf indexes are merged in the resulting list.
I'm having a bit of trouble figuring out the best way to traverse and merge the data. From the above data set the Expected Output is:
[[1, 2, 3, 4, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [5, 20, 21]]
Fixed Attempt, seems to work, is there a more efficient method?
class MergeMachine(object):
processed = []
def merge(self, idx, parent_indexes, existing):
if idx not in self.processed:
parent_indexes.append(idx)
if self.data[idx]["root"]:
for related_root_idx in self.data[idx]["root"]:
if related_root_idx not in self.processed and related_root_idx not in parent_indexes:
existing.extend(self.merge(related_root_idx, parent_indexes, existing))
self.processed.append(related_root_idx)
existing.append(idx)
existing.extend(self.data[idx]["leaf"])
self.processed.append(idx)
return existing
def process(self, data):
results = []
self.data = data
for root_idx in self.data.keys():
r = set(self.merge(root_idx, [], []))
if r:
combined = False
for result_set in results:
if not r.isdisjoint(result_set):
result_set.union(r)
combined = True
if not combined:
results.append(r)
return results
mm = MergeMachine()
mm.process(data)
Is there a efficient way to merge the data into the expected output?