Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Removing duplicate elements from a Python list containing unhashable elements while preserving order?
- [
- [('A', '1'), ('B', '2')],
- [('A', '1'), ('B', '2')],
- [('A', '4'), ('C', '5')]
- ]
- [
- [('A', '1'), ('B', '2')],
- [('A', '4'), ('C', '5')]
- ]
- sample = []
- sample.append([('A', '1'), ('B', '2')])
- sample.append([('A', '1'), ('B', '2')])
- sample.append([('A', '4'), ('C', '5')])
- from operator import itemgetter
- from itertools import groupby, imap
- def unique_justseen(iterable, key=None):
- "List unique elements, preserving order. Remember only the element just seen."
- # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
- # unique_justseen('ABBCcAD', str.lower) --> A B C A D
- return imap(next, imap(itemgetter(1), groupby(iterable, key)))
- from bisect import bisect_left, insort
- def dedup(seq):
- 'Remove duplicates. Preserve order first seen. Assume orderable, but not hashable elements'
- result = []
- seen = []
- for x in seq:
- i = bisect_left(seen, x)
- if i == len(seen) or seen[i] != x:
- seen.insert(i, x)
- result.append(x)
- return result
- def unique(a):
- indices = sorted(range(len(a)), key=a.__getitem__)
- indices = set(next(it) for k, it in
- itertools.groupby(indices, key=a.__getitem__))
- return [x for i, x in enumerate(a) if i in indices]
- >>> a = ['F', 'J', 'B', 'F', 'V', 'A', 'E', 'U', 'B', 'U', 'Z', 'K']
- >>> unique(a)
- ['F', 'J', 'B', 'V', 'A', 'E', 'U', 'Z', 'K']
- seen = set()
- output = []
- for e in yourlist:
- t = tuple(e)
- if t in seen:
- continue
- else:
- seen.add(t)
- output.append(e)
- # return output
Add Comment
Please, Sign In to add comment