From 50831666840ec2ee065569c3e3c9fbc5461fc918 Mon Sep 17 00:00:00 2001 From: Travis Oliphant Date: Wed, 19 Jul 2006 16:19:00 +0000 Subject: Apply patch for unique from #154 --- numpy/lib/function_base.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'numpy/lib/function_base.py') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 44c31b8ec..374901ccd 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -491,17 +491,28 @@ def trim_zeros(filt, trim='fb'): else: last = last - 1 return filt[first:last] -def unique(inseq): - """Return unique items (in sorted order) from a 1-dimensional sequence. - """ - # Dictionary setting is quite fast. - set = {} - for item in inseq: - set[item] = None - val = asarray(set.keys()) - val.sort() - return val +import sys +if sys.hexversion < 0x2040000: + from sets import Set as set + +def unique(x): + """Return sorted unique items from an array or sequence. + + Example: + >>> unique([5,2,4,0,4,4,2,2,1]) + array([0,1,2,4,5]) + """ + 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). -- cgit v1.2.1