hello,

I am trying to create a nested tree loop between months , days and hours controlled by an outer slider, that can control the nested tree loop (in python grasshopper) as an example:

month m: 1,2,3

day d: 1 2 3

hours h: 10, 13, 16

external slider from zero to 27

the expected outcome for this tree nested loop is:

when the external slider is zero

the output is : [1,1,10]

when the slider is 1

the output is :[1,1,13]

when the slider is 2

the output is:[1,1,16]

when the slider is 3

the output is : [1,2,10]

when the slider is 4

the output is:[1,2,13]

ect…

so how could I reach this part??

any help?

thank you

Best regards

Fatima.b

Hi Fatima, How is this?

```
months = [1,2,3]
days = [1,2,3]
hours = [10,13,16]
def output(n): return [months[n//9], days[(n%9)//3], hours[n%3]]
```

best regards,

Graham

Dear Graham,

Thank you so much for your fast reply

I will try it

Best Regards

Fatima B

Dear Grahm,

I have try it but it did not give me the result that I have mention it in my previous question. the result is now null

the result that i want is

[1,1,10]

[1,1,13]

[1,1,16]

[1,2,10]

[1,2,13]

[1,2,16]

[1,3,10]

…[3,3,16]

Kindly check the file below

and what do you mean by n . n is what ?

nested loop controled by u.gh (7.0 KB)

thank you

best regards

Hello Fatima,

*n* is your slider value - I see you have used *u* for this

Using the file you sent I have simply replaced the Python script block with `b = [x[u//9], y[(u%9)//3], z[u%3]]`

This seems to work with the exception of value 27 : you only need inputs from 0 to 26

Dear Graham,

Thank you sooo much it is working

best regards

fatima.b

1 Like

Dear Graham,

If I want to add another parameter for example

in addition to the hours,days and months I ant to add angle (which are(0,30,60,90))

so we have 4 different parameters

so the output that i wanted is

[1,1,10,0]

[1,1,10,30]

[1,1,10,60]

[1,1,10,90]

[1,1,13,0]

[1,1,13,30]

[1,1,13,60]

[1,1,13,90]

so I should add to the formula which wasb = [x[u//9], y[(u%9)//3], z[u%3]]

since I have an additional parametrs that is the angles

Best regards

Fatima b

Hello,

yes you need to add a 4th input `d = [0,30,60,90]`

to calculate the list indices you could use

```
ix = u//36
iy = (u//12)%3
iz = (u%12)//4
id = u%4
b = [x[ix], y[iy], z[iz], d[id]]
```

This is all using modular arithmatic to calculate the list indices.

Dear Graham

Thank you again and again

it was really helpful

now it is working

but I need to understand these formula for example why % and why we use // ? there is any way that can help me to understand it?

Best regards

Fatima B

You’re very welcome

Modular arithmetic is really useful for this type of thing so it is worth working on it, especially if you need to work with large data sets. I would start by writing out the full list and thinking about where you need to start counting from (using %) and what step size you need to take (using //)

It is a little opaque though!!! A slower but clearer solution would be using nested loops to build a list of all possible answers and then just outputting the one you need:

```
outputs = []
for month in x:
for day in y:
for hour in z:
for angle in d:
outputs.append([month,day,hour,angle])
b = outputs[u]
```