I’m using Python to see if a list contains multiple strings. It works great if I’m searching for multiple strings but errors if there is only 1 string. Is there something I can add to my python node to accommodate this?
# Load the Python Standard and DesignScript Libraries
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
list = IN[0]
out = []
for sublist in list:
if True in sublist:
out.append(True)
else:
out.append(False)
OUT = out
If you are trying to determine if an item of list 1 also occurs in list 2 you could use the following approaches, no need for Python, that only makes it unnecessarily complex in my opinion in this case:
# Load the Python Standard and DesignScript Libraries
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
stringList = IN[0]
out = []
def is_nested_list(list2Check):
try:
next(x for x in list2Check if isinstance(x,list))
except StopIteration:
return False
return True
if is_nested_list(stringList):
for sublist in stringList:
if True in sublist:
out.append(True)
else:
out.append(False)
else:
if True in stringList:
out.append(True)
else:
out.append(False)
"""
for sublist in list:
if sublist.__contains__(True):
out.append(True)
else:
out.append(False)
"""
OUT = out