[SciPy-user] remove duplicate points...

Michael mnandris at btinternet.com
Wed Apr 23 12:33:36 EDT 2008


Hello list, 

I don't know a 'numponic' way of doing this, but have found it useful in
the past to tuplise the data, then eliminate duplicates by keying them
into a dictionary. Maybe this routine could be adapted using built-in
numpy routines. Most plotting packages accept tuples no problem (pylab
does).

d = {}

with_duplicates = [ [xn, yn, zn, vn], ... ]  

for k,v in enumerate( with_duplicates ):  
    d[v] = tuple(k)

without_duplicates = d.keys() 

Possible the only known use of tuples that is actually usefull!

Incidentally, what _are_ python tuples for (and why aren't they called
'allelles? :) - they're immutable, gedit?) 

I approve of the existence of tuples, on philosophical grounds alone; i
just don't know what they're for; i never encountered a burning need for
them, apart from the above.


On Wed, 2008-04-23 at 11:09 -0400, Angus McMorland wrote:
> Hi Fred et al,
> 
> On 23/04/2008, fred <fredmfp at gmail.com> wrote:
> > Hi,
> >
> >  I have array of 3D points:
> >
> >  [x0, y0, z0, v0]
> >  [x1, y1, z1, v1]
> >  ...
> >  [xn, yn, zn, vn]
> 
> >  This array have duplicate elements (points), by construction.
> >
> >  How could I remove these duplicate elements ?
> 
> I had to do this prior to Delaunay triangulation at one point, and
> wrote a quick routine to remove the duplicates, which works on the
> assumption that unique pts will have a unique product (x * y * z). I
> doubt this is very efficient, but usually when I post these sorts of
> answers it stimulates the gurus to post better solutions, which is
> always good.
> 
> import numpy as np
> 
> def remove_dups(ptsar, atol=1e-8):
>     '''removes duplicate entries from pts array'''
>     prods = ptsar.prod(axis=1)
>     sorted = ptsar[prods.argsort()]
>     prodsorted = prods[prods.argsort()]
>     diffs = np.greater(np.absolute(np.diff(prodsorted)), atol)
>     diffs = np.hstack(((True,), diffs))
>     return sorted[diffs]
> 
> I hope that helps,
> 
> A.




More information about the SciPy-User mailing list