Error selecting data from a list in python if/else

Hello, Guys

I already wrote about a similar question a couple of years ago.

now I’m back to this python scraper.
for some reason an error occurs when using

I will explain with an example, see the picture
for example, there is a value 76 in the list and a pair of numbers 140 and 140 correspond to it
in python there is a condition that if there are 76 in the list, and 140 in parallel to it, then you need to write values 143 to the output list, and it writes 152 from the next list.

I don’t understand what is the problem.

if_else error.gh (24.7 KB)

Your first Data component has 2496 values in branches of two, meaning 1248 pairs. You graft it and you get 2496 branches of single values.
The second Data component features 1248 single values in individual branches.
Since you have your Python component inputs set to List Input, this means that the code inside the component is run for each individual branch of the tree, since each branch is a list. Now you’re left 2496 values to pair with 1248 values which is problematic, since only half of the 2496 values have a corresponding value.

As you can see the component runs 2495 cycles starting from 0, which translated to running the code 2496 times.

Screenshot 2023-02-07 at 08.05.36

If at each cycle you want to process a pair of values from the first Data component with a single value from the second Data component simply don’t graft the first one!

Screenshot 2023-02-07 at 08.00.17

Now the component runs 1247 cycles starting at 0, which seems more appropriate.
Since you know that at each cycle, two values of x_in and one value of y_in get evaluated together, you have to refactor your code to something like this:

a = []

xran = (100, 120, 140, 160, 180, 200, 210, 220, 230, 240, 250, 260, 270, 280, 300)

xres = [(97,108,119,129,140,150,160,170,180,190,200,210,220,230,240,251,276,301),     # 48 
        (97,108,119,129,140,150,160,170,180,190,200,210,220,230,240,251,276,301),     # 60 
        (120,132,143,154,165,176,181,186,191,196,202,207,212,217,227),     # 76 
        (127,140,152,163,174,185,190,195,200,206,211,216,221,226,237),     # 89 
        (134,145,156,167,178,189,199,209,220,230,240,250,261,271,281,291,316,341),    # 102 
        (129,140,152,163,174,185,195,205,216,226,237,247,257,267,277,287,313,338),    # 114 
        (181,195,208,219,229,241,252,263,273,284,294,304,315,325,335,345,371,396),    # 133
        (173,189,203,215,227,238,250,261,271,282,292,303,313,324,334,344,370,395),    # 159
        (253,253,253,253,267,279,292,303,315,326,337,348,359,369,380,390,416,442),    # 219
        (357,357,357,357,357,357,357,357,357,357,370,383,394,406,418,429,456,483),    # 245
        (357,357,357,357,357,357,357,357,357,357,370,383,394,406,418,429,456,483),    # 273
        (430,430,430,430,430,430,430,430,430,430,430,430,430,430,442,455,484,513)]    # 325
yran = [48,60,76,89,102,114,133,159,219,245,273,325]  

y = y_in[0]

for x in x_in:
    res = 500
    if y in yran:
        idx_y = yran.index(y)
        if x in xran:
            idx_x = xran.index(x)
            res = xres[idx_y][idx_x]
    a.append(res)

if_else error-rev.gh (25.1 KB)

2 Likes

thank you very much thank you beams :+1: :grinning:

1 Like