Split list into equal partitions, with random items in each partition

I want to pseudo-randomly select items from a list and put them into equal-length branches without any duplicates.

I’m wondering if this can be done simply with native Gh components? Currently, I’m doing it with a python script, but that just seems so unnecessary. The last group can have less than the others, accounting for a list length that isn’t perfectly divisible by the number of divisions.

Yes.

Can you explain how you would go about doing it? Thanks

Where is your GH file / data / geometry?

1 Like

Here’s the important bit (x is geo, y is # of divisions):

import random as ra
import ghpythonlib.treehelpers as th

output = []
div = int(len(x)/y)
for i in range(y):
    temp = []
    for j in range(div):
        if x:
            num = ra.randrange(0, len(x)-1)
            if num == 0:
                pass
            else:
                temp.append(x[num])
                x.pop(num)
            
            
        else:
            pass
    
    output.append(temp)

output.append(x)

output = th.list_to_tree(output, source=[0,0])
a = output

3. Attach minimal versions of all the relevant files

I am done here, you’re welcome.

If anyone else comes here looking for the answer, it is as simple as jittering the list and then partitioning that new list. Not sure why I didn’t think of that in the first place.