Search for multiple strings values in list

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