Can i make this ReplacebyCondition, StringContains script smaller?

Hello everyone,

I have setup this script to replace Strings by other Strings if the old String contains the corresponding name.
The problem with this setup is the size in my Dynamo, i have over 100 strings that need converting this way and as you can see my script will get huge if i use this method.

I hope someone knows a more optimized solution!
Thanks in advance!

ReplaceByCondition 12-11-2019.dyn (35.6 KB)

String.Replace and ReplaceByCondition do not run concurrently but there may be other options in modifying your string values more efficiently. Python would be the easiest but it’s possible to do with nodes (though this depends on your exact requirements.)

Look into how your strings are constructed. If they’re setup with a nice naming convention like yours seem to be you can usually apply some logic to recreating the name rather than replacing all the different substrings. A dictionary will also help with the different types/sizes.

2 Likes

Hey Nick i am using Dynamo 2.0 but it seems that your code block script doesn’t function for me, it crashes my Dynamo.

Hey @daan, please replace the { } with [ ] :slight_smile: Shifting from Dynamo 1.X to 2.X we changed some of the DesignScript code - lists used to be created using curly braces, but are now using square ones - with curly braces being utilized in dictionaries.

Yeah, dictionaries changed in 2.0. Make sure you’re using the correct syntax as @solamour pointed out, but if that doesn’t work you can use the node from the Springs package.

@solamour @Nick_Boyts Hey yes i used the square brackets, but it was the second most right, the “Dict…” code block, but this didnt work. I fixed it with a Clockwork Package node :slight_smile:

1 Like

We also have out of the box dictionary nodes too :slight_smile: Like below, and callable in DesignScript.

4 Likes

@Daan alternatively, here is a python option using dictionary functionality generated from simple inputs. :wink:

#Inputs
referencelist = IN[0]
searchvalues = IN[1]
replacevalues = IN[2]

#Containers
output = []
dict = {}

#Make a Dictionary for Serach and Replace Values
for sv,rep in zip(searchvalues,replacevalues):
	dict[sv]=rep

#Loop Search and Replace through list
for r in referencelist:
	output.append(reduce(lambda a, kv: a.replace(*kv), dict.iteritems(), r))

#Result
OUT = output
4 Likes