Search for multiple strings values in list

Hi all,

I’ve a list of elements, based on the element name I want to filter out dynamic list of strings. Now its just 2 string, but in the future its also possible that this will be more.

When I do this with the node string contains I got only the result for the value ‘isolatie’. I could of course do cross product, but then I’ve to combine the number of nested lists into 1 list.

So I was thinking to do a python script. But I don’t know what I’m doing wrong here. Can someone help me with this? Thanks in advance!

Here’s a solution just with nodes. I wasn’t sure whether you wanted to find names that contained one or more of the search strings, or ones that contained all of the search strings so both are there. It’s scalable, in that you can add more search strings to the list.

In the Python node, there’s a few issues:

  • When you zip the lists, you’re only comparing the first string to the first element, and so on. You need to loop first through the elements, and then loop through the search strings within the element loop
  • You’re comparing the element itself, rather than its name to the string, which will be false because they are not the same datatype
  • The equality operator == is looking for identical objects. You’re looking for a substring, so you could instead use the form if searchString in elemName: to find whether the name contains the search string

Hope this helps,
Thomas

3 Likes

Hi @Thomas_Corrie,

thanks for the reply, but you’re going a little fast here. What you’re saying is that I need to do a nested loop, right? Something like this?

image

About the == operator, I’m comparing the i.Name (the name of the element - datatype: string (right?)) with the search string(s), right? I don’t understand what you mean with if searchString in elemName. Should ‘if’ not be ‘for’? Or can you do a if statement like this?!

Sorry for this, I’m quite new with python. Thanks for your help!

KR Merlijn

Hi @merlijnvanleeuwen,

I glossed quickly over the Python code because I thought the node-only solution did what you needed but happy to expand the Python option. Assuming you want elements that meet one or more of the search string tests then how about this:

You could get the element names within the Python node, but to save unwrapping the Revit elements I have assumed you get the name outside of Python and feed it into IN[1], with the actual elements in IN[2]. I’m working with Sandbox here so I’ve mocked up elements as strings but the Python code is the same.

The == operator compares all of the name to the search string, so assuming your search string is only part of the name, then it will not be completely equal. To find substrings within strings you can use the form if searchString in Name and it will return true if the searchString can be found
https://docs.python.org/2/reference/expressions.html#membership-test-details
Conversely if searchString not in Name will return true if the searchString cannot be found

strList = IN[0]
elementNames = IN[1] # Getting the names in Dynamo before Python saves unwrapping
elementList = IN[2]

searchList = []
restList = []

for n, e in zip(elementNames,elementList): # zip together names and elements
	found = False # assume search strings can't be found
	for s in strList: # Loop through the search strings
		if s in n: # test whether the string can be found in the name
			searchList.append(e) # if true, add to searchList
			found = True # set found to be true
			break # skip remaining tests as string has been found
	if not found: # if found is false after testing all strings
		restList.append(e) # append to restList

# Assign your output to the OUT variable.
OUT = searchList, restList

Hope this helps,
Thomas

*EDIT

If you wanted to find only elements where the name contained all search strings then you could use this code instead:

strList = IN[0]
elementNames = IN[1] # Getting the names in Dynamo before Python saves unwrapping
elementList = IN[2]

searchList = []
restList = []

for n, e in zip(elementNames,elementList): # zip together names and elements
	found = True # assume search strings are all found
	for s in strList: # Loop through the search strings
		if s not in n: # test whether the string is not found in the name
			found = False # set found to False
	if found: # if found is True
		searchList.append(e) # append to searchList	
	else:
		restList.append(e) # append to restList	

# Assign your output to the OUT variable.
OUT = searchList, restList

2 Likes

@Thomas_Corrie, yes!!! I got it. Thanks a lot

1 Like