Python code to return total count of no. of positions in which items are differing at same index
<p>A=[1,2,3,4,5,6,7,8,9]
B=[1,2,3,7,4,6,5,8,9]</p>

<p>I have to compare these two lists and return the count of no. of location in which items are differing using one line python code.</p>

<p>For example: 
the output should be 4 for given arrays because at index (3,4,5,6) the items are differing.So, program should return 4.</p>

<p>My way of doing this is comparing each and every location using for loop:</p>

<pre><code>count=0
for i in range(0,len(A)):
   if(A[i]==B[i]):
     continue
   else:
     count+=1
print(count)
</code></pre>

<p>Please help me in writing one line python code for this.</p>

<p>There are <a href="http://stackoverflow.com/questions/28663856/how-to-count-the-occurrence-of-certain-item-in-an-ndarray-in-python">many ways</a> to do this. If you're using numpy, you could just use <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.count_nonzero.html" rel="nofollow"><code>np.count_nonzero</code></a>:</p>

<pre><code>&gt;&gt;&gt; a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
&gt;&gt;&gt; b = np.array([1, 2, 3, 7, 4, 6, 5, 8, 9])
&gt;&gt;&gt; a != b
array([False, False, False,  True,  True, False,  True, False, False], dtype=bool)
&gt;&gt;&gt; np.count_nonzero(a != b)
3
</code></pre>

<p>Note that <code>a != b</code> returns an <em>array</em> containing true and false depending upon how the condition evaluates at each index.</p>

<p>Here's a speed comparison:</p>

<pre><code>&gt;&gt;&gt; %timeit np.count_nonzero(a != b)
The slowest run took 40.59 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 752 ns per loop

&gt;&gt;&gt; %timeit sum(i != j for i, j in zip(a, b))
The slowest run took 5.86 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 18.5 Âµs per loop
</code></pre>

<p>The caching obscures the timing, but <code>40.59 * 0.752 = 30.52Âµs</code>, while <code>5.86 * 18.5 = 108.41Âµs</code>, so numpy's slowest still seems significantly faster than pure python's slowest run.</p>

<p>This is much clearer with larger arrays:</p>

<pre><code>&gt;&gt;&gt; n = 10000
&gt;&gt;&gt; a = np.arange(n)
&gt;&gt;&gt; b = np.arange(n)
&gt;&gt;&gt; k = 50
&gt;&gt;&gt; ids = np.random.randint(0, n, k)
&gt;&gt;&gt; a[ids] = 0
&gt;&gt;&gt; ids = np.random.randint(0, n, k)
&gt;&gt;&gt; b[ids] = 0
&gt;&gt;&gt; %timeit np.count_nonzero(a != b)
The slowest run took 20.50 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 11.5 Âµs per loop
&gt;&gt;&gt; %timeit sum(i != j for i, j in zip(a, b))
100 loops, best of 3: 15.6 ms per loop
</code></pre>

<p>The difference is much more stark, with numpy taking <em>at most</em> 235 <em>micro</em>-seconds, while pure python takes 15.6 <em>milli</em>-seconds on <em>average</em>!</p>

python arrays python-2.7 python-3.x numpy