2

我使用了这个代码,但是这个代码只删除了列表中的重复项并返回没有重复项的列表,例如,如果我有 a = [1,1,2,3,3,4,6] 当我使用底部代码时给出输出这个 a = [1,2,3,4,6]。但我只想输出只出现一次的整数我想要这个 [2,4,6] 任何人都可以帮助pleaseee熬夜试图解决这个问题

def unique(a):
    order = set()
    order_add = order.add
    return [x for x in a if x not in order and not order_add(x)]
4

1 回答 1

2

要在删除具有重复项的项目时保留顺序:

>>> from collections import Counter
>>> x = [1, 2, 3, 2, 1, 8]
>>> counts = Counter(x)
>>> [item for item in x if counts[item] == 1]
[3, 8]
于 2012-10-14T04:54:58.557 回答