Python:从列表中删除特定项目的重复项

问题描述:

我有一个项目列表,我想在其中删除一个项目的所有重复项,但保留其余项目的任何重复项.IE.我从下面的列表开始

I have a list of item, where I want to remove the occurrence of any duplicates for one item, but keep any duplicates for the rest. I.e. I start with the following list

mylist = [4, 1, 2, 6, 1, 0, 9, 8, 0, 9]

我想删除 0 的所有重复项,但保留 1 9 的重复项.我当前的解决方案如下:

I want to remove any duplicates of 0 but keep the duplicates of 1 and 9. My current solution is the following:

mylist = [i for i in mylist if i != 0]
mylist.add(0)

除了以下几种以外,是否还有一种很好的方法可以保持 0 的出现?

Is there a nice way of keeping one occurrence of 0 besides the following?

for i in mylist:
    if mylist.count(0) > 1:
        mylist.remove(0)

在本示例中,第二种方法花费的时间是原来的两倍以上.

The second approach takes more than double the time for this example.

说明:

  • 当前,我不在乎列表中的项目顺序,因为我目前正在创建和清除该项目后对其进行排序,但是稍后可能会更改.

  • currently, I don't care about the order of items in the list, as I currently sort it after it has been created and cleaned, but that might change later.

当前,我只需要删除一个特定项目的重复项(在我的示例中为 0 )

currently, I only need to remove duplicates for one specific item (that is 0 in my example)

解决方案:

[0] + [i for i in mylist if i]

看起来足够好,除非 0 不在 mylist 中,在这种情况下,您错误地添加了0.

looks good enough, except if 0 is not in mylist, in which case you're wrongly adding 0.

此外,添加2个这样的列表并不是很好的性能选择.我会做的:

Besides, adding 2 lists like this isn't very good performance wise. I'd do:

newlist = [i for i in mylist if i]
if len(newlist) != len(mylist):  # 0 was removed, add it back
   newlist.append(0)

(或使用过滤器 newlist = list(filter(None,mylist))可能会稍快一些,因为没有本地python循环)

(or using filter newlist = list(filter(None,mylist)) which could be slightly faster because there are no native python loops)

在最后一个位置附加到列表非常有效( list 对象使用预分配,并且大多数情况下不复制内存).长度测试技巧是 O(1),可以避免在mylist中测试 0

Appending to a list at the last position is very efficient (list object uses pre-allocation and most of the time no memory is copied). The length test trick is O(1) and allows to avoid to test 0 in mylist