Replace Multiple Search Strings

thank you very much, i am an overwhelmed beginner :slight_smile: still having trouble here…

for some reason the list.filterbyboolmask gives me 197 positions and the string.replace gives me only 8 positions back. I understand it probably comes from the list I created that contains 8 items to be searched, but i dont know how to solve the problem… I tried Cross Product and the string replace multiple but was not succesful either… Thank you very much!

Not an expert, but i think you should use List.Combine in addition to String.Replace. I proceed that way when I have an occurrence of a family on Revit of which I want to change multiple parameters.

Hi @bialmeida

Try changing lacing to longest for String.Replace node:

Thanks… I kind of understand what happens but I dont know why or how to fix it again - looks better than before though! Revit didnt replace the items until number 7, that coincidentally has a string value “NUF_Shops”, the same as the 7. item of my list.create. Number 6 NUF-Mall wasnt replaced either…

In case you want to do it fast and clean, you can use this python script:

listreplace

list = IN[0]
newlist = []

for x in list:
    if "NUF" in x:
        newlist.append(1)
    else:
        newlist.append(x)

OUT = newlist
2 Likes

Take a look at this (concerns the different type of lacings) : http://dynamoprimer.com/en/06_Designing-with-Lists/6-1_whats-a-list.html

I don’t really think that you this is about longest lacing here, and I think that you should use List.Combine. I’ll try to make a quick example.

Edit : replace multiple strings.dyn (15.1 KB)

Ok, unfortunately, couldn’t get to do it with only List.Combine, but here’s another way. A python way, like luca suggested, might be the best way to go :slight_smile:

2 Likes

@bialmeida @mellouze @lucamanzoni Here is another possible way using dictionary.

gifgif

6 Likes

Thank you very much, that works quite well!

I tried to extend the list so that other strings get replaced as well and had to take the “else” part away, otherwise some strings would get duplicated, but it still works fine… I would be happy to know how to fix it though, just in case you have an idea…


1 Like

Thank you very very much! Do you guys have a recommendation where to learn python and dynamo?

Hi :slight_smile:

The best way to learn Dynamo is to go through the Primer (and testing by yourself obvisouly :slight_smile: ) : http://primer.dynamobim.org/en/

Regarding Python, I went through this course (but it’s in French) : https://openclassrooms.com/fr/courses/235344-apprenez-a-programmer-en-python

1 Like

If you have more conditions, you need to use elif, otherwise it doesn’t work: in both pictures you get a different number of items than the input.

Also I used in because I wanted to check if a string is contained. If you know exactly the value you are looking for, just use ==

list = IN[0]
newlist = []

for x in list:
    if "NUF" in x:
        newlist.append(1)
    elif x == "def":
        newlist.append(2)
    else:
        newlist.append(x)

OUT = newlist
2 Likes

@bialmeida @lucamanzoni Here is another method just few lines :sunglasses: to avoid if or else:

5 Likes

That works perfectly!! Thank you very very very much!!

1 Like

it does not work with sublists as the string input

I stumbled across this problem and this is my solution (using any and list comprehension):

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


list = IN[0]
newlist = []

specialCharacter=["(",")",";",","]
for a in list:
    if any([x in a for x in specialCharacter]):
        a=a.replace("(","")
        a=a.replace(")","")
        a=a.replace(",","")
        a=a.replace(";","")
        newlist.append(a)
    else:
        newlist.append(a)

OUT = newlist