假设我有:
x=[a,b,a,b,c,d]
我想要一个方法来获取
y=[c,d]
我已经设法做到了:
for i in x:
if x.count(i) == 1:
unique.append(i)
问题是,这对于更大的列表来说非常慢,有帮助吗?
最佳答案
先用一个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]
关于Python 删除列表中的重复项(而不是保留它们),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30675296/