In short what I am trying to do is based on a list objects = ["bison", "elephant", "horse", "ibis", "sky", "mountain", "building", "flower", "sand", "tree", "field", "road", "tower", "ocean", "cliff", "waterfall"]
get the number 1 if that list of strings contains is substring of the elements from another list otherwise 0
For instance, I have another list name as Lista =['dusthaze-sky', 'rocky-mountain']
since it contains sky and mountain
It should return [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
What I have till now is the following code
Lista = ['dusthaze-sky', 'rocky-mountain']
objects = ["bison", "elephant", "horse", "ibis", "sky", "mountain", "building", "flower", "sand", "tree", "field", "road", "tower", "ocean", "cliff", "waterfall"]
returnList = []
for obj in objects:
for element in lista: returnList.append(1) if any(map(obj in element, obj)) else returnList.append(0)
Which is giving me the folowing error
Traceback (most recent call last):
File "main.py", line 22, in <module>
printBoolArray(readObjects)
File "main.py", line 10, in printBoolArray
for m in lista: returnList.append(1) if any(map(n in m, m)) else returnList.append(0)
TypeError: 'bool' object is not callable
Hopefully there is something that can be done and if possible I would like it to be done using the any function
Perhaps something along these lines will work for you:
Lista = ['dusthaze-sky', 'rocky-mountain']
objects = ["bison", "elephant", "horse", "ibis", "sky", "mountain", "building", "flower", "sand", "tree", "field", "road", "tower", "ocean", "cliff", "waterfall"]
returnList = []
for obj in objects:
if any(obj in elem for elem in Lista):
returnList.append(1)
else:
returnList.append(0)
print(returnList)
Out: [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Note, this is not the most efficient way of doing this, but the solution might get complicated if we try to optimize it. Feel free to drop a comment if you have questions.
And here is a one liner if you like list comprehension:
returnList = [int(any(obj in elem for elem in Lista)) for obj in objects]