You could do this using numpy by doing something like this, the mergsort is stable so it'll let you pick out the first or last occurrence of each value:
def unique(array, orderby='first'):
array = np.asarray(array)
order = array.argsort(kind='mergesort')
array = array[order]
diff = array[1:] != array[:-1]
if orderby == 'first':
diff = np.concatenate([[True], diff])
elif orderby == 'last':
diff = np.concatenate([diff, [True]])
else:
raise ValueError
uniq = array[diff]
index = order[diff]
return uniq[index.argsort()]
This answer is very similar to:
def unique(array):
uniq, index = np.unique(array, return_index=True)
return uniq[index.argsort()]
But, numpy.unique uses an unstable sort internally so you're not guaranteed to get any specific index, ie first or last.
I think an ordered dict might also work:
def unique(array):
uniq = OrderedDict()
for i in array:
uniq[i] = 1
return uniq.keys()