如何通过带有 Python 的映射基于任何函数获取 1 和 0 的列表...我收到错误对象不可调用
How to get a list of 1's and 0's based on any function through a map with Python... I am getting the error object is not callable
简而言之,我想做的是基于一个列表 objects = ["bison", "elephant", "horse", "ibis", "sky", "mountain", "building", "flower", "sand", "tree", "field", "road", "tower", "ocean", "cliff", "waterfall"]
如果该字符串列表包含的是另一个列表中元素的子字符串,则获取数字 1 否则为 0
例如,我有另一个列表名称 Lista =['dusthaze-sky', 'rocky-mountain']
因为它包含天空和山脉
应该return[0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
我现在只有下面的代码
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)
这是给我以下错误
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
希望有什么可以做的,如果可能的话,我希望使用 any 函数
也许这些方面的内容对您有用:
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]
请注意,这不是执行此操作的最有效方法,但如果我们尝试对其进行优化,解决方案可能会变得复杂。如果您有任何问题,请随时发表评论。
如果你喜欢列表理解,这里有一行:
returnList = [int(any(obj in elem for elem in Lista)) for obj in objects]
简而言之,我想做的是基于一个列表 objects = ["bison", "elephant", "horse", "ibis", "sky", "mountain", "building", "flower", "sand", "tree", "field", "road", "tower", "ocean", "cliff", "waterfall"]
如果该字符串列表包含的是另一个列表中元素的子字符串,则获取数字 1 否则为 0
例如,我有另一个列表名称 Lista =['dusthaze-sky', 'rocky-mountain']
因为它包含天空和山脉
应该return[0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
我现在只有下面的代码
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)
这是给我以下错误
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
希望有什么可以做的,如果可能的话,我希望使用 any 函数
也许这些方面的内容对您有用:
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]
请注意,这不是执行此操作的最有效方法,但如果我们尝试对其进行优化,解决方案可能会变得复杂。如果您有任何问题,请随时发表评论。
如果你喜欢列表理解,这里有一行:
returnList = [int(any(obj in elem for elem in Lista)) for obj in objects]