Python checking equality of tuples
<p>I have a numpy array of source and destination ip's</p>

<pre><code>consarray
array([['10.125.255.133', '104.244.42.130'],
   ['104.244.42.130', '10.125.255.133']], dtype=object)
</code></pre>

<p>The actual array is much larger than this.</p>

<p>I want to create a set of unique connection pairs from the array:</p>

<p>In the given eg: it is clear that both rows of the numpy array are part of same connection (Just src and destination are interchanged, so it is outgoing and incoming respectively).</p>

<p>I tried creating a set of unique tuples.
like this:</p>

<pre><code>conset = set(map(tuple,consarray))
conset
{('10.125.255.133', '104.244.42.130'), ('104.244.42.130', '10.125.255.133')}
</code></pre>

<p>What i actually want is for ('10.125.255.133', '104.244.42.130') and ('104.244.42.130', '10.125.255.133') to be considered the same so that only one of them will be in the set.</p>

<p>Can anyone tell me how do i go about doing this?</p>

<p><strong>EDIT:</strong></p>

<p>There have been some good answers, but actually i want another requirement,</p>

<p>I want that the first occurrence should always be the one retained irrespective of the ip address.</p>

<p>In the above example: ('10.125.255.133', '104.244.42.130') appears first, so it is the outgoing connection, i want to retain this.</p>

<p>If the above example changed to:</p>

<pre><code>consarray
array(['104.244.42.130', '10.125.255.133']],
    [['10.125.255.133', '104.244.42.130'],dtype=object)
</code></pre>

<p>I would want ('104.244.42.130', '10.125.255.133') to be retained.</p>

<p>Since you're using <code>numpy</code>, you can use <code>numpy.unique</code>, eg:</p>

<pre><code>a = np.array([('10.125.255.133', '104.244.42.130'), ('104.244.42.130', ' 10.125.255.133')])
</code></pre>

<p>Then <code>np.unique(a)</code> gives you:</p>

<pre><code>array(['10.125.255.133', '104.244.42.130'], dtype='&lt;U14')
</code></pre>

python tuples