a = [1,2,3,4,5]
b = [1,3,5,6]
c = a and b
print c
Run Code Online (Sandbox Code Playgroud)
实际产出:[1,3,5,6]
预期产量:[1,3,5]
我们如何在两个列表上实现布尔AND操作(列表交集)?
对于两个列表,
a = [1, 2, 9, 3, 8, ...] (no duplicate values in a, but a is very big)
b = [1, 9, 1,...] (set(b) is a subset of set(a), 1<<len(b)<<len(a))
indices = get_indices_of_a(a, b)
Run Code Online (Sandbox Code Playgroud)
如何让get_indices_of_a回indices = [0, 2, 0,...]用array(a)[indices] = b?有没有比使用更快的方法a.index,这需要太长时间?
制作b一集是匹配的列表,并返回指数的快速方法(见的匹配值的Python和回报指数比较两个列表),但它会失去第二的指标1,以及在这种情况下,指数序列.