diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-07-19 16:19:00 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-07-19 16:19:00 +0000 |
commit | 50831666840ec2ee065569c3e3c9fbc5461fc918 (patch) | |
tree | b78da6fbbdd235fb2216763e0511ce75b4b9dde1 /numpy/lib/function_base.py | |
parent | 0991c5d70f87c38a92364da8485be265762686db (diff) | |
download | numpy-50831666840ec2ee065569c3e3c9fbc5461fc918.tar.gz |
Apply patch for unique from #154
Diffstat (limited to 'numpy/lib/function_base.py')
-rw-r--r-- | numpy/lib/function_base.py | 31 |
1 files changed, 21 insertions, 10 deletions
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). |