如果我有 2 个列表,在 Python 中说:
l1 = ['a', 'b', 'c', 'd']
l2 = ['c', 'd', 'e']
有没有办法找出它们有多少相同的元素。在这种情况下,它将是 2(c 和 d)
我知道我可以做一个嵌套循环,但是没有像 php 中那样带有 array_intersect 函数的内置函数吗
谢谢
最佳答案
您可以为此使用集合交集:)
l1 = ['a', 'b', 'c', 'd']
l2 = ['c', 'd', 'e']
set(l1).intersection(l2)
set(['c', 'd'])
关于python - 在 Python 中查找 2 个列表中相同元素的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2500124/