来源于这篇文章

从序列中删除重复项的最快方法取决于序列元素的一些非常细微的属性,例如它们是否可哈希以及它们是否支持完全比较。 unique()函数尝试三种方法,从最快到最慢,让运行时异常选择可用于当前序列的最佳方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def unique(s):
'''
for best speed, all sequence elements should be hashble.
Then unique() will usually work in linear time.

if not possible, the swquenct elements should enjoy a total ordering,
and if `list(s).sort()` doesn't raise TypeError, it is assumed that they
do enjoy a total ordering.
Then unique() will usually work in O(N*logN) time.

If that's not possible either, the sequence elements must support
equality-testing. Then unique() will usually work in quadratic
time.
'''
n = len(s)
if n == 0:
return []

# try using a dict first, as that is the fastest and usually work
u = {}
try:
for x in s:
u[x] = 1
except TypeError:
del u # move on to the next method
else:
return u.keys()

try:
t = list(s)
t.sort()
except TypeError:
del t # move on to the next method
else:
assert n > 0
last = t[0]
lasti = i = 1
while i < n:
if t[i] != last:
t[lasti] = last = t[i]
lasti += 1
i += 1
return t[:lasti]

u = []
for x in s:
if x not in u:
u.append(x)
return u