http://www.cnblogs.com/moinmoin/archive/2011/09/26/python-remove-list-repeat-hash.html
1.在可hash的情况下使用set,时间复杂度为 O(n)
|
1
|
return
list
(
set
(s))
|
2.不可hash,但支持比较使用sort,时间复杂度为 O(nlogn)
|
1
2
3
4
5
6
7
|
t
=
list
(s)
try
:
t.sort()
except
TypeError:
del
t
else
:
return
[x
for
i,x
in
enumerate
(t)
if
not
i
or
t[i]!
=
t[i
-
1
]]
|
3.前两者都不能的情况下利用in判断,时间复杂度为 O(n**2)
|
1
2
3
4
5
|
u
=
[]
for
x
in
s:
if
x
not
in
u:
u.append(x)
return
u
|
本文介绍了三种在Python中去除列表重复元素的方法:1. 使用set针对可哈希元素;2. 利用sort针对可排序元素;3. 通过逐个检查元素是否已存在针对不可哈希也不可排序的元素。
3813

被折叠的 条评论
为什么被折叠?



