Replace Multiple Search Strings


#1

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!


String into Number
String into Number
#2

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.


#3

Hi @bialmeida

Try changing lacing to longest for String.Replace node:


#4

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…


#5

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

#6

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:


#7

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

gifgif


#8

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…



#9

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


#10

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


#11

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

#12

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


#13

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