删除列表中的重复项

def remove_duplicates():
    t = ['a', 'b', 'c', 'd']
    t2 = ['a', 'c', 'd']
    for t in t2:
        t.append(t.remove())
    return t

答案

>>> 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]
>>> from collections import OrderedDict
>>> list(OrderedDict.fromkeys(t))
[1, 2, 3, 5, 6, 7, 8]
>>> list(dict.fromkeys(t))
[1, 2, 3, 5, 6, 7, 8]
>>> from collections import OrderedDict
>>> list(OrderedDict.fromkeys('abracadabra'))
['a', 'b', 'r', 'c', 'd']
>>> list(dict.fromkeys('abracadabra'))
['a', 'b', 'r', 'c', 'd']
>>> list(dict.fromkeys('abracadabra'))
['a', 'b', 'r', 'c', 'd']
from collections import OrderedDict
OrderedDict((x, True) for x in source_list).keys()
>>> t = [1, 2, 3, 1, 2, 5, 6, 7, 8]
>>> t
[1, 2, 3, 1, 2, 5, 6, 7, 8]
>>> s = []
>>> for i in t:
       if i not in s:
          s.append(i)
>>> s
[1, 2, 3, 5, 6, 7, 8]
def remove_duplicates(l):
    return list(set(l))

制作一个新列表,保留L中重复项的第一个元素的顺序

newlist=[ii for n,ii in enumerate(L) if ii not in L[:n]]

例如, if L=[1, 2, 2, 3, 4, 2, 4, 3, 5] newlist if L=[1, 2, 2, 3, 4, 2, 4, 3, 5]newlist将为[1,2,3,4,5]

在添加新元素之前,这会检查每个新元素之前是否未出现在列表中。而且它不需要进口。

def ordered_set(in_list):
    out_list = []
    added = set()
    for val in in_list:
        if not val in added:
            out_list.append(val)
            added.add(val)
    return out_list
from random import randint
x = [randint(0,100) for _ in xrange(100)]

In [131]: len(set(x))
Out[131]: 62
In [129]: %timeit list(OrderedDict.fromkeys(x))
10000 loops, best of 3: 86.4 us per loop

In [130]: %timeit ordered_set(x)
100000 loops, best of 3: 15.1 us per loop
def ordered_set(inlist):
    out_list = []
    for val in inlist:
        if not val in out_list:
            out_list.append(val)
    return out_list
In [136]: %timeit ordered_set(x)
10000 loops, best of 3: 52.6 us per loop
t=['a','a','b','b','b','c','c','c']
t2= ['c','c','b','b','b','a','a','a']
import pandas as pd
pd.unique(t).tolist()
>>>['a','b','c']
pd.unique(t2).tolist()
>>>['c','b','a']
import numpy as np
np.unique(t).tolist()
>>>['a','b','c']
np.unique(t2).tolist()
>>>['a','b','c']
_, idx = np.unique(t2, return_index=True)
t2[np.sort(idx)].tolist()
>>>['c','b','a']
>>> seq = [1,2,3,'a', 'a', 1,2]
>> dict.fromkeys(seq).keys()
['a', 1, 2, 3]
myList = [1, 2, 3, 1, 2, 5, 6, 7, 8]
cleanlist = []
[cleanlist.append(x) for x in myList if x not in cleanlist]
>>> cleanlist 
[1, 2, 3, 5, 6, 7, 8]