import rhinoscriptsyntax as rs
import ghpythonlib.components as ghc
import Rhino.Geometry as rg
list = ['a','b','c','d','e','f']
#num = [2,3,1]
num = 2
a = ghc.PartitionList(list,num)
I don’t really know what’s happening with PartionList, but a quick workaround would be a rather simple function that makes use of list splicing, like so:
from ghpythonlib import treehelpers
def split(lt, size=2, out_tree=False):
nested_lt = []
while len(lt) > size:
seg = lt[:size]
nested_lt.append(seg)
lt = lt[size:]
nested_lt.append(lt)
if not out_tree:
return nested_lt
return treehelpers.list_to_tree(nested_lt)
I’ve tested the script and it seems to work exactly like the vanilla component!
from ghpythonlib import treehelpers
def partition(lt, size=[2,3,2], tree_out=False):
"""Partitions a list into sub-lists.
Args:
lt: A list to partition
size: Optional list of partition sizes
tree_out: Optional, if True a tree is returned,
by default a nested list is put out.
Returns:
A partioned list or tree.
"""
nested_lt = []
n = 0
while len(lt) > size[n]:
sub_lt = lt[:size[n]]
nested_lt.append(sub_lt)
lt = lt[size[n]:]
if n == len(size)-1:
n = 0
else:
n += 1
nested_lt.append(lt)
if tree_out:
return treehelpers.list_to_tree(nested_lt)
return nested_lt
C = partition(L, S, True)
One thing to consider is that you always need to input a list of sizes, even if you want a regular partition rhythm (e.g. [ [0, 1], [2, 3], [4, 5], [6, 7] ]), you pass in the single size (e.g. 2) as a list (e.g. [2]) !
you can get ghpythonlib.components to do what you are after.
Be default, and always in Rhino 5, ghpythonlib flattens results that are given as trees. This was done for simplicity, but it turned out that sometimes trees are also useful. For example, in this case.
You can use the trees variable to get access to all trees-aware functions.