Revit Array to String

I need to get from the list in the Watch window (see images) to a List like this:

INSERT INTO tblRevitInstancesMaterials (RevitInstanceID, MaterialID) VALUES (‘751744’,’612’)

INSERT INTO tblRevitInstancesMaterials (RevitInstanceID, MaterialID) VALUES (‘751744’,’794693’)

INSERT INTO tblRevitInstancesMaterials (RevitInstanceID, MaterialID) VALUES (‘751744’,’2077197’)

INSERT INTO tblRevitInstancesMaterials (RevitInstanceID, MaterialID) VALUES (‘751744’,’2081627’)

INSERT INTO tblRevitInstancesMaterials (RevitInstanceID, MaterialID) VALUES (‘751795,’612’)

INSERT INTO tblRevitInstancesMaterials (RevitInstanceID, MaterialID) VALUES (‘751795,’794693’)

INSERT INTO tblRevitInstancesMaterials (RevitInstanceID, MaterialID) VALUES (‘751795,’2077197’)

INSERT INTO tblRevitInstancesMaterials (RevitInstanceID, MaterialID) VALUES (‘751795,’2081627’)

Is the final output supposed to be a string ? I think the easiest way would be through a Python node (i.e. loop thourgh the list in the left to create the (‘A’,‘B’) part and then just add to it the initial part by hand).

Yes, the final output should be a string. However, I am brand new to Dynamo with no Python experience. Any examples I could follow?

Thanks!

Try to learn some Python with what you can find online, that will be useful during your Dynamo journey :slight_smile:

numbers = IN[0]

result = []

for i in range(len(numbers)):
    for j in range(len(numbers[i][1])):
        result.append("INSERT INTO tblRevitInstancesMaterials (RevitInstanceID, MaterialID) VALUES ('"+str(numbers[i][0])+"',"+str(numbers[i][1][j])+"')");

OUT = result
3 Likes

Hey, sorry for the dumb question, could you spell out to me what exactly the numbers[i][1] bit is doing?

I get that it is defining the length of the range, but not how it does it!

Thanks :slight_smile:

Mark

Thanks! This is exactly what I needed. Seems like Python will be very useful!

Just have to figure out why I am getting this warning:

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
unexpected token ‘;’

Probably a typo, check that you are doing a : after the ))

Hi Mark :slight_smile:

range(len(numbers)) gives the length of the overall list @L4 (in my example 2 --> range(2) = [0,1]).

range(len(numbers[i][1])) gives the length of the last layer of sublists. Here, I loop through the entire list @L4, then each sublist @L2 : the [1] part is only here because of the unique structure of the list @L4. Here, the length of the last sublists are 4 and 4.

I don’t know if that’s clear enough :confused:

Yeah sorry, as @Mark.Ackerley said, it was a typo (EDIT : two typos actually). I corrected the script. Please refer to the screenshot of the script, it is correct.

1 Like

Awesome! Thanks!

So you are able to use ‘1’ because @ level 2, the item is always at position 1… however you don’t know how many items there might be @ level 4 so you need to use [i] to iterate through them all? I would never have realised that they were in that order…! Thanks!

Exactly !

i refers to the index of the items at @L3, so i must be between 0 and the length of the big list @L4.
Then, we know for sure about the structure of each sublist @L3, so we can use [0] and [1].
Finally, we don’t know how many items there are in each sublist @L2, so j refers to those indexs.

Note that numbers[i][0][j] does not appear in the script, as numbers[i][0] is a int and not a list, so numbers[i][0][j] does not make any sense for Python.

1 Like