Python pairwise comparison of elements in a array or list
<p>Let me elaborate my question using a simple example.I have a=[a1,a2,a3,a4], with all ai being a numerical value. </p>

<p>What I want to get is pairwise comparisons within 'a', such as 
I(a1>=a2), I(a1>=a3), I(a1>=a4), ,,,,I(a4>=a1), I(a4>=a2), I(a4>=a3), where I is a indicator function. So I used the following code.</p>

<pre><code>res=[x&gt;=y for x in a for y in a]
</code></pre>

<p>But it also gives the comparison results like I(a1>=a1),..,I(a4>=a4), which is always one. To get rid of these nuisance, I convert res into a numpy array and find the off diagonal elements.</p>

<pre><code>res1=numpy.array(res)
</code></pre>

<p>This gives the result what I want, but I think there should be more efficient or simpler way to do pairwise comparison and extract the off diagonal element. Do you have any idea about this? Thanks in advance.</p>

<p>You could use <a href="http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html" rel="nofollow"><code>NumPy broadcasting</code></a> -</p>

<pre><code># Get the mask of comparisons in a vectorized manner using broadcasting
mask = a[:,None] &gt;= a

# Select the elements other than diagonal ones
out = mask[~np.eye(a.size,dtype=bool)]
</code></pre>

<p>If you rather prefer to set the diagonal elements as <code>False</code> in <code>mask</code> and then <code>mask</code> would be the output, like so -</p>

<pre><code>mask[np.eye(a.size,dtype=bool)] = 0
</code></pre>

<p>Sample run -</p>

<pre><code>In [56]: a
Out[56]: array([3, 7, 5, 8])

In [57]: mask = a[:,None] &gt;= a

In [58]: mask
Out[58]: 
array([[ True, False, False, False],
       [ True,  True,  True, False],
       [ True, False,  True, False],
       [ True,  True,  True,  True]], dtype=bool)

In [59]: mask[~np.eye(a.size,dtype=bool)] # Selecting non-diag elems
Out[59]: 
array([False, False, False,  True,  True, False,  True, False, False,
        True,  True,  True], dtype=bool)

In [60]: mask[np.eye(a.size,dtype=bool)] = 0 # Setting diag elems as False

In [61]: mask
Out[61]: 
array([[False, False, False, False],
       [ True, False,  True, False],
       [ True, False, False, False],
       [ True,  True,  True, False]], dtype=bool)
</code></pre>

<hr>

<p><strong>Runtime test</strong> </p>

<p>Reasons to use <code>NumPy broadcasting</code>? Performance! Let's see how with a large dataset -</p>

<pre><code>In [34]: def pairwise_comp(A): # Using NumPy broadcasting    
    ...:     a = np.asarray(A) # Convert to array if not already so
    ...:     mask = a[:,None] &gt;= a
    ...:     out = mask[~np.eye(a.size,dtype=bool)]
    ...:     return out
    ...: 

In [35]: a = np.random.randint(0,9,(1000)).tolist() # Input list

In [36]: %timeit [x &gt;= y for i,x in enumerate(a) for j,y in enumerate(a) if i != j]
1 loop, best of 3: 185 ms per loop # @Sixhobbits's loopy soln

In [37]: %timeit pairwise_comp(a)
100 loops, best of 3: 5.76 ms per loop
</code></pre>

python arrays numpy comparison rank