通过比较2个列表来创建布尔列表

时间:2019-06-17 08:10:52

标签: python list filter boolean

我有2个列表要比较。 List_A包含我拥有的所有文件名,List B包含我想要的文件名。

作为输出的List_C是一个布尔列表,其长度与List_A相同。对于在List_B中找不到的文件显示false,对于在List_B中发现的文件显示true。

这是用于通过布尔值过滤元素列表。

# 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_A = IN[0]
List_B = IN[1]



OUT = List_C

A scenario

List_A = [x1, x2, x3, x4, x5]

List_B = [x2, x3]

List_C = [false, true, true, false, false]

2 个答案:

答案 0 :(得分:1)

这是映射,而不是过滤,它是单行的:

list(map(lambda x: x in List_B, List_A))

您可以使用列表推导

[x in List_B for x in List_A]

但是我个人发现在两种不同的意义上看到x in令人困惑。

答案 1 :(得分:0)

使用列表理解:

List_C = [elem in List_B for elem in List_A]

这基本上等于for循环,它为List_A的每个元素将elem in List_B的结果附加到新列表中。

相关问题