python列表去掉特定项,Python:从列表中删除特定项目的重复项

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]

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)

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.

Clarification:

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.

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

解决方案

The solution:

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

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

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)

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值