简体   繁体   English

从列表中删除重复项,包括原始匹配项

[英]Remove duplicates from list, including original matching item

I tried searching and couldn't find this exact situation, so apologies if it exists already. 我尝试搜索并找不到这种确切的情况,所以如果它已经存在就道歉。

I'm trying to remove duplicates from a list as well as the original item I'm searching for. 我正在尝试从列表中删除重复项以及我正在搜索的原始项目。 If I have this: 如果我有这个:

ls = [1, 2, 3, 3]

I want to end up with this: 我想最终得到这个:

ls = [1, 2]

I know that using set will remove duplicates like this: 我知道使用set会删除像这样的重复项:

print set(ls)  # set([1, 2, 3])

But it still retains that 3 element which I want removed. 但它仍然保留了我要删除的3元素。 I'm wondering if there's a way to remove the duplicates and original matching items too. 我想知道是否有办法删除重复项和原始匹配项。

Use a list comprehension and list.count : 使用list comprehension和list.count

>>> ls = [1, 2, 3, 3]
>>> [x for x in ls if ls.count(x) == 1]
[1, 2]
>>>

Here is a reference on both of those. 以下是对这两者的参考


Edit: 编辑:

@Anonymous made a good point below. @Anonymous在下面提出了一个很好的观点。 The above solution is perfect for small lists but may become slow with larger ones. 上述解决方案适用于小型列表,但对于较大的列表可能会变慢。

For large lists, you can do this instead: 对于大型列表,您可以这样做:

>>> from collections import Counter
>>> ls = [1, 2, 3, 3]
>>> c = Counter(ls)
>>> [x for x in ls if c[x] == 1]
[1, 2]
>>>

Here is a reference on collections.Counter . 这是关于collections.Counter的参考。

If items are contigious, then you can use groupby which saves building an auxillary data structure in memory...: 如果项目是有条件的,那么你可以使用groupby来节省在内存中构建辅助数据结构...:

from itertools import groupby, islice

data = [1, 2, 3, 3]
# could also use `sorted(data)` if need be...
new = [k for k, g in groupby(data) if len(list(islice(g, 2))) == 1]
# [1, 2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM