a = [1,2,3,4,5] b = [1,3,5,6] c = a and b print c
实际输出:[1,3,5,6] 预期输出:[1,3,5]
[1,3,5,6]
[1,3,5]
我们如何在两个列表上实现布尔AND操作(列表交集)?
如果顺序不重要,并且你不必担心重复,则可以使用set相交:
>>> a = [1,2,3,4,5] >>> b = [1,3,5,6] >>> list(set(a) & set(b)) [1, 3, 5]