[Numpy-discussion] unique() should return a sorted array

Norbert Nemec Norbert.Nemec.list at gmx.de
Sun Jul 2 04:47:11 CDT 2006


I agree.

Currently the order of the output of unique is undefined. Defining it in
such a way that it produces a sorted array will not break any compatibility.

My idea would be something like

def unique(arr,sort=True):
    if hasattr(arr,'flatten'):
        tmp = arr.flatten()
        tmp.sort()
        idx = concatenate([True],tmp[1:]!=tmp[:-1])
        return tmp[idx]
    else: # for compatibility:
        set = {}
        for item in inseq:
            set[item] = None
        if sort:
            return asarray(sorted(set.keys()))
       else:
            return asarray(set.keys())

Does anybody know about the internals of the python "set"? How is
.keys() implemented? I somehow have really doubts about the efficiency
of this method.


David Huard wrote:
> Hi,
>
> Numpy's unique(x) returns an array x with repetitions removed.
> However, since it returns asarray(dict.keys()), the resulting array is
> not sorted, worse, the original order may not be conserved. I think
> that unique() should return a sorted array, like its matlab homonym.
>
> Regards,
>
> David Huard
> ------------------------------------------------------------------------
>
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> ------------------------------------------------------------------------
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>   





More information about the Numpy-discussion mailing list