Hi,

I attached a patch for unique (with a test case) based on Norbert's suggestion. I removed the sort keyword since iterable arrays would be sorted anyway. The function uses a python set, and I was wondering if it is ok to assume that everyone running numpy has a python version >= 2.3 ?

David
Index: numpy/lib/tests/test_function_base.py
===================================================================
--- numpy/lib/tests/test_function_base.py	(revision 2791)
+++ numpy/lib/tests/test_function_base.py	(working copy)
@@ -353,8 +353,19 @@
         (a,b)=histogram(linspace(0,10,100))
         assert(all(a==10))
 
+class test_unique(NumpyTestCase):
+    def check_simple(self):
+        x = array([4,3,2,1,1,2,3,4, 0])
+        assert(all(unique(x) == [0,1,2,3,4]))
+        assert(unique(array([1,1,1,1,1])) == array([1]))
+        # from ??? import inf
+        #x = array([-0.5, -4.2, 0., inf, -5, -4.2, 0, -1/2., inf])
+        #assert(all(unique(x) == [-5, -4.2, -0.5, 0, inf]))
+        x = ['widget', 'ham', 'foo', 'bar', 'foo', 'ham']
+        assert(all(unique(x) ==  ['bar', 'foo', 'ham', 'widget']))
+        x = array([5+6j, 1+1j, 1+10j, 10, 5+6j])
+        assert(all(unique(x) == [1+1j, 1+10j, 5+6j, 10]))
 
-
 def compare_results(res,desired):
     for i in range(len(desired)):
         assert_array_equal(res[i],desired[i])
Index: numpy/lib/function_base.py
===================================================================
--- numpy/lib/function_base.py	(revision 2791)
+++ numpy/lib/function_base.py	(working copy)
@@ -474,17 +474,23 @@
             else: last = last - 1
     return filt[first:last]
 
-def unique(inseq):
-    """Return unique items (in sorted order) from a 1-dimensional sequence.
+def unique(x):
+    """Return sorted unique items from a one dimensional sequence.
+
+    Example:
+    >>> unique([5,2,4,0,4,4,2,2,1])
+    array([0,1,2,4,5])
     """
-    # Dictionary setting is quite fast.
-    set = {}
-    for item in inseq:
-        set[item] = None
-    val = asarray(set.keys())
-    val.sort()
-    return val
-
+    try:
+        tmp = x.flatten()
+        tmp.sort()
+        idx = concatenate(([True],tmp[1:]!=tmp[:-1]))
+        return tmp[idx]
+    except AttributeError:
+        items = list(set(x))
+        items.sort()
+        return asarray(items)
+        
 def extract(condition, arr):
     """Return the elements of ravel(arr) where ravel(condition) is True
     (in 1D).
-------------------------------------------------------------------------
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@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion

Reply via email to