I got stuck with create python scripts for classify data from excel (IF ELSE and FOR LOOP)

Im rather new in using dynamo in revit and I try to create custom node for classify data coming from excel the code is as follow

import clr
import math
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import*

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager

doc = DocumentManager.Instance.CurrentDBDocument

#The inputs to this node will be stored as a list in the IN variables.
id = IN[0]
x = IN[1]
y = IN[2]
Blevel = IN[3]
Tlevel = IN[4]
b = IN[5]
h = IN[6]
typemark = IN[7]
covering = IN[8]
rebarno = IN[9]
rebarsize = IN[10]
layout = IN[11]
stirrupsize = IN[12]

one = []
two = []
three = []
four = []

for i in layout:
    a1 = []
    a2 = []
    a3 = []
    a4 = []

    if layout == "Single":
        for j in id:
            if j == i:
                a1 = id
            else:
                n = id

    elif layout == "2-Bundle":
        for j in id:
            if j == i:
                a2 = id
            else:
                n = id
            
    elif layout == "3-Bundle":
        for j in id:
            if j == i:
                a3 = id
            else:
                n = id
            
    else:
        for j in id:
            if j == i:
                a4 = id
            else:
                n = id
            
            
        one.append(a1)
        two.append(a2)
        three.append(a3)
        four.append(a4)

#Assign your output to the OUT variable.
OUT = one,two,three,four

I got total of 13 input that start from IN[0] to IN[12] each input represent each data category

id = IN[0]
x = IN[1]
y = IN[2]
Blevel = IN[3]
Tlevel = IN[4]
b = IN[5]
h = IN[6]
typemark = IN[7]
covering = IN[8]
rebarno = IN[9]
rebarsize = IN[10]
layout = IN[11]
stirrupsize = IN[12]

and I would like to test using layout as criteria.

This is my id (double) = [367776,367909,367926,367943,367958,367973]

in list format

List 0
1 367776
2 367909
3 367926
4 367943
5 367958
6 367973

and my layout = [“Single”,“2-Bundle”,“3-Bundle”,“4-Bundle”,“Single”,“Single”]

in list format

List 11
1 Single
2 2-Bundle
3 3-Bundle
4 4-Bundle
5 Single
6 Single

what I expected when I run these python scripts

for i in layout:
    a1 = []
    a2 = []
    a3 = []
    a4 = []

    if layout == "Single":
        for j in id:
            if j == i:
                a1 = id
            else:
                n = id

    elif layout == "2-Bundle":
        for j in id:
            if j == i:
                a2 = id
            else:
                n = id
            
    elif layout == "3-Bundle":
        for j in id:
            if j == i:
                a3 = id
            else:
                n = id
            
    else:
        for j in id:
            if j == i:
                a4 = id
            else:
                n = id
            
            
        one.append(a1)
        two.append(a2)
        three.append(a3)
        four.append(a4)

#Assign your output to the OUT variable.
OUT = one,two,three,four

the result should be

one = [367776,367958,367973]
two = [367909]
three = [367926]
four = [367943]

in list format

List 0
1 367776
2 367958
3 367973
List 1
1 367909
List 2
1 367926
List 3
1 367943

after I run these scripts I got no error or warning but the result is not as I expected these scripts didnt give out any information and I dont know where I went wrong. I just try to classify only element id first.

Welcome to the community!

Could you please share with us the excel and dynamo files?

The Python code is not very clear. Try to use image this when pasting your Python code.


Due to the fact that I cant upload file just yet I place link that I upload both dynamo file and excel file on google drive. sorry for unclear scripts

I am not quite sure what you are trying to do. Do you want to make a custom node that classifies data according to a specific column the user specifies? or you are just interested in grouping the Ids according to layout? if that is the case, then you do not need Python. “List.GroupByKey” will do it for you

Of course, you can change the list to whatever you want. Also the keys!

classify.dyn (20.5 KB)

I don’t follow exactly what you’re doing but it looks like you have too many loops. If you’re trying to sort elements by layout type it should be as simple as this:

for element_layout_id in element_layout_ids:
     if element_layout_id == layout_id_1:
          output.append(layout_type_1)
     elif element_layout_id == layout_id_2:
          output.append(layout_type_2)
     elif element_layout_id == layout_id_3:
          output.append(layout_type_3)

thank you I got it but i may need to have a little adjustment to classify multiple data at a time

1 Like

thank you let me try it later because I more preferred to have short python scripts instead of long path dynamo but Im too newbie to write working scripts.