python列表去重复后按照顺序_Python列表:这是删除重复同时保留顺序的最佳方式吗?...

I've read a lot of methods for removing duplicates from a python list while preserving the order. All the methods appear to require the creation of a function/sub-routine, which I think is not very computationally efficient.

I came up with the following and I would like to know if this is the most computationally efficient method to do so?

(My usage for this has to be the most efficient possible due to the need to have fast response time.) Thanks

b=[x for i,x in enumerate(a) if i==a.index(x)]

解决方案

a.index(x) itself will be O(n) as the list has to be searched for the value x. The overall runtime is O(n^2).

"Saving" function calls does not make a bad algorithm faster than a good one.

More efficient (O(n)) would probably be:

result = []

seen = set()

for i in a:

if i not in seen:

result.append(i)

seen.add(i)

(the top answer also shows how to do this in a list comprehension manner, which will be more efficient than an explicit loop)

You can easily profile your code yourself using the timeit [docs] module. For example, I put your code in func1 and mine in func2. If I repeat this 1000 times with an array with 1000 elements (no duplicates):

>>> a = range(1000)

>>> timeit.timeit('func1(a)', 'from __main__ import func1, a', number=1000)

11.691882133483887

>>> timeit.timeit('func2(a)', 'from __main__ import func2, a', number=1000)

0.3130321502685547

Now with duplicates (only 100 distinct values):

>>> a = [random.randint(0, 99) for _ in range(1000)]

>>> timeit.timeit('func1(a)', 'from __main__ import func1, a', number=1000)

2.5020430088043213

>>> timeit.timeit('func2(a)', 'from __main__ import func2, a', number=1000)

0.08332705497741699

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值