unique(seq, stable=False): return a list of the elements in seq in arbitrary order, but without duplicates. If stable=True it keeps the original element order (using slower algorithms).
(seq, stable=False)
| 3 | |
| 4 | |
| 5 | def unique(seq, stable=False): |
| 6 | """unique(seq, stable=False): return a list of the elements in seq in arbitrary |
| 7 | order, but without duplicates. |
| 8 | If stable=True it keeps the original element order (using slower algorithms).""" |
| 9 | # Developed from Tim Peters version: |
| 10 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560 |
| 11 | |
| 12 | #if uniqueDebug and len(str(seq))<50: print "Input:", seq # For debugging. |
| 13 | |
| 14 | # Special case of an empty s: |
| 15 | if not seq: return [] |
| 16 | |
| 17 | # if it's a set: |
| 18 | if isinstance(seq, set): return list(seq) |
| 19 | |
| 20 | if stable: |
| 21 | # Try with a set: |
| 22 | seqSet= set() |
| 23 | result = [] |
| 24 | try: |
| 25 | for e in seq: |
| 26 | if e not in seqSet: |
| 27 | result.append(e) |
| 28 | seqSet.add(e) |
| 29 | except TypeError: |
| 30 | pass # move on to the next method |
| 31 | else: |
| 32 | #if uniqueDebug: print "Stable, set." |
| 33 | return result |
| 34 | |
| 35 | # Since you can't hash all elements, use a bisection on sorted elements |
| 36 | result = [] |
| 37 | sortedElem = [] |
| 38 | try: |
| 39 | for elem in seq: |
| 40 | pos = bisect_left(sortedElem, elem) |
| 41 | if pos >= len(sortedElem) or sortedElem[pos] != elem: |
| 42 | insort_left(sortedElem, elem) |
| 43 | result.append(elem) |
| 44 | except TypeError: |
| 45 | pass # Move on to the next method |
| 46 | else: |
| 47 | #if uniqueDebug: print "Stable, bisect." |
| 48 | return result |
| 49 | else: # Not stable |
| 50 | # Try using a set first, because it's the fastest and it usually works |
| 51 | try: |
| 52 | u = set(seq) |
| 53 | except TypeError: |
| 54 | pass # move on to the next method |
| 55 | else: |
| 56 | #if uniqueDebug: print "Unstable, set." |
| 57 | return list(u) |
| 58 | |
| 59 | # Elements can't be hashed, so bring equal items together with a sort and |
| 60 | # remove them out in a single pass. |
| 61 | try: |
| 62 | t = sorted(seq) |