Python在列表中删除重复项(而不是保留它们)

时间:2015-06-05 20:27:31

标签: python

说我有:

x=[a,b,a,b,c,d]

我想要一种方法来获取

y=[c,d]

我设法用计数来做:

 for i in x:
   if x.count(i) == 1:
     unique.append(i)

问题是,对于更大的列表来说,这是非常慢的,帮助?

3 个答案:

答案 0 :(得分:4)

首先使用dict来计算:

d = {}
for i in x:
    if i not in d:
        d[i] = 0
    d[i] += 1
y = [i for i, j in d.iteritems() if j == 1]

答案 1 :(得分:3)

x=["a","b","a","b","c","d"]

from collections import Counter

print([k for k,v in Counter(x).items() if v == 1])    
['c', 'd']

或者为了保证订单首先创建Counter dict,然后迭代x列表执行值的查找,只保留值为1的k:

x = ["a","b","a","b","c","d"]
from collections import Counter

cn = Counter(x)
print([k for k in x if cn[k] == 1])

所以一次通过x来创建dict,另一次通过理解,给出一个整体0(n)解决方案,而不是使用count的二次方法。

Counter dict计算每个元素的出现次数:

In [1]: x = ["a","b","a","b","c","d"]    
In [2]: from collections import Counter    
In [3]: cn = Counter(x)    
In [4]: cn
Out[4]: Counter({'b': 2, 'a': 2, 'c': 1, 'd': 1})
In [5]: cn["a"]
Out[5]: 2  
In [6]: cn["b"]
Out[6]: 2    
In [7]: cn["c"]
Out[7]: 1

执行cn[k]会返回每个元素的计数,因此我们最终只保留c和d。

答案 2 :(得分:-3)

执行此操作的最佳方法是使用set()函数,如下所示:

x=['a','b','a','b','c','d']
print list(set(x))

因为set()函数返回无序结果。使用sorted()函数,可以解决此问题:

x=['a','b','a','b','c','d']
print list(sorted(set(x)))