更好,更短的方法:
>>> a = [1, 2, 9, 5, 1]>>> b = [9, 8, 7, 6, 5]>>> len(set(a) & set(b)) # & is intersection - elements common to both2
为什么您的代码不起作用:
>>> def filter_(x, y):... count = 0... for num in y:... if num in x:... count += 1... return count... >>> filter_(a, b)2
您
return count在for循环内,返回时未完成执行。



