How to convert 2D list(list in list) to tree-data structure?

Hello guys,

As the title, and I searched many solutions but failed about trying.
So pls help me finishing the attached GHpython code.
This Tree Data converting has been confused me for long long time, hope this time could
throughly understand it.

convert to (8.6 KB)

Thanks a lot!!

There are plenty of threads on this topic search the forums

is this Rhino6? if so, the builtin ghpythonlib.treehelpers takes care of this for you.

import ghpythonlib.treehelpers as th

store = []

for i in index:
    if i == '0': store.append(replace[0])
    elif i == '1': store.append(replace[1])
    elif i == '2': store.append(replace[2])
    elif i == '3': store.append(replace[3])
    elif i == '4': store.append(replace[4])

r = len(store)
for i in range(r):
    store[i] = [store[i]]*length[i]  #2d list, aka, a list in list

#how to convert a list in list to data-tree structure?

a = th.list_to_tree(store)
1 Like

Hi Chris,

Perfectly done!
It`s great in R6 has this much simplified method to solve the problem!
However I still wanna to know how to do it in R5 in a, you know, more traditional way:P
Here is bit of code:

import clr

from Grasshopper.Kernel.Data import GH_Path
from Grasshopper import DataTree

dataTree = DataTree[object]()
for i, thing in enumerate(data):
branch_id = branch_ids[i]
path = GH_Path(branch_id)
dataTree.Add(thing, path)

a = dataTree

I will keep working on it by the mean time I will much appriaciated if you got time on this.

Thank you again!


There is some relevant discussion here: