Boolean Mask that filters strings containing substrings made of digits-letters sequences; multiple filtering of strings

Hi,
I just made a script in Dynamo that gets all the FamilySymbols (aka the Revit “Types”) inside of a project, and filters their parent families’ names that contain a certain substring, creating a new list of FamilySymbols on which I apply the SetParameterByName node.

The purpose of the script is creating a list of FamilySymbols (Types) of a certain category that follow a name codification (that represents the specific function of each type), so that I can set the shared (type) parameter (that the client gave me) of these Types with a string that describes their function. In this example, the script filters the Types whose name contains the substring “PM” and then sets the shared parameter “HT_Cluster” (type parameter) to “POMPE” (“pumps”: see screenshot attached that shows the result).

Now I’d like to refine the script. Two questions:

  1. How can I filter the names of the parent families that contain a substring made of a sequence of random letters and digits? For example, I want a filtering that takes all the names (strings) in a list that contain a sequence like “LLLLDD_”, where “L” is a letter and “D” is a digit.

  2. How can I obtain a Boolean Mask that filters the names of the parent families that have a substring OR another. For example, I want to filter all the names (strings) in a list that contain the substring “HBV”, OR the substring “HFV”, OR the substring “HGV”.

I attach the graph and the script too. Thank you in advance!

SetMEC-Equipment_V1_POMPE.dyn (52.7 KB)

  1. Look into RegEx (regular expressions). It’s used for exactly this. There are some custom packages that handle RegEx but I don’t remember which ones. You can also do it in Python.

  2. Use an OR statement for your 3 conditions. You can either do this for the full condition individually or use list levels to check each substring and then check your returned sublists for a TRUE value. The latter should be more flexible to a varying number of inputs.

2 Likes

Agree with Nick…

Maybe this is helpful…

Cheers,

Mark

1 Like

Thank you for your help Nick, I will try these solutions asap! I figured that Python would handle these situations in a more compact way, I’m still learning it but I want to try it one day. For question 2, I was thinking of using a code block with OR statements too and work with levels, I’ll post the solution once I find it.

Thank you Mark, that graph shows exactly what I was looking for! I’ll post the solution asap! I’ll also try those nodes from the Clockwork package, they seem cool. Thanks for the link too!

1 Like

I’m still developing the script, just for your interest I tried to create a small Python script that does exactly the same thing of the graph by Mark. It works well so far!

Hi again, sorry for the spam. I think I’ve found the perfect solution using a Python script, I’m asking anyone who actually uses Python to check the validity of this script :sweat_smile:.

Basically, I have a list of names of families as first input, and a second input of substrings; I want to check if a name has either the substring “HT_” or the substring “3T_” .

The script creates a boolean mask made of: “true”, if the name matches ANY of the two substrings, and “false” otherwise.

To make it work, I make a list of sublists: each sublist has the result of the comparison between the n-th family name and the k-th substring. Basically it looks like:

((true, false…(1st name)), (false, false…(2nd name)), … (results for the nth name)).

Then, if ALL the values of the sublist are false, the script appends “false” in the “mask” list, otherwise it appends “true”. This is the final boolean mask I was looking for, and I can use it to filter the names that don’t contain any substring.

Here is the same script updated with the new Python script, and the script itself. I also post the .dyn.
I think it’s logically solid, if there are any observations please provide feedback. Thank you!

SetMEC-Equipment_V2_POMPE.dyn (47.1 KB)

Good job. I have one recommendation just to help clean things up. Rather than testing all your conditions and then checking to see if any of them pass you can just check them once. An easy way to do this is to default your test value to False and then check each condition. If you meet a condition you set the test value to True.

1 Like

Good suggestion, I’ll try that! Thank you!

I am having an error while running this,
To set the parameter Value Can u please let me know what to do for the error