How to find list intersection?

前端 未结 12 2499
别那么骄傲
别那么骄傲 2020-11-22 05:21
a = [1,2,3,4,5]
b = [1,3,5,6]
c = a and b
print c

actual output: [1,3,5,6] expected output: [1,3,5]

How can we ac

12条回答
  •  情书的邮戳
    2020-11-22 05:30

    Using list comprehensions is a pretty obvious one for me. Not sure about performance, but at least things stay lists.

    [x for x in a if x in b]

    Or "all the x values that are in A, if the X value is in B".

提交回复
热议问题