Compare lists to find common elements in python
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/642763/python-intersection-of-two-lists">Python - Intersection of two lists</a>  </p>
</blockquote>



<p>i'm trying to compare two lists in order to find the number of elements they have in common.</p>

<p>The main problem I'm having is when either list contains repeated elements, for example</p>

<pre><code>A = [1,1,1,1]   and  
B = [1,1,2,3]
</code></pre>

<p>using the code</p>

<pre><code>n = 0
for x in A:
   if x in B:
      n += 1
print n
</code></pre>

<p>gives me the output that <code>n = 4</code>, as technically all elements of A are in B</p>

<p>I'd like to get the output that <code>n = 2</code>, preferably without using sets, Is there anyway I can adapt my code, or a new way of thinking about the problem to achieve this? </p>

<p>Thanks</p>
