python: delete the duplicates in a list
下面有几种做法, 其中3之简洁令人惊讶.
1,
>>> t = [1, 2, 3, 1, 2, 5, 6, 7, 8]
>>> t
[1, 2, 3, 1, 2, 5, 6, 7, 8]
>>> list(set(t))
[1, 2, 3, 5, 6, 7, 8]
>>> s = [1, 2, 3]
>>> list(set(t) - set(s))
[8, 5, 6, 7]
2,
def f7(seq):
seen = set()
seen_add = seen.add
return [x for x in seq if not (x in seen or seen_add(x))]
3,
myList = sorted(set(myList))
4,
for i in mylist:
if i not in newlist:
newlist.append(i)