Python列表:这是删除重复同时保留顺序的最佳方式吗? [英] Python List: Is this the best way to remove duplicates while preserving order?

查看:160
本文介绍了Python列表:这是删除重复同时保留顺序的最佳方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能的重复:

如何从Python列表中删除重复项,同时保留顺序?

算法 - 如何有效地删除列表中的重复元素?

Possible Duplicates:
How do you remove duplicates from a list in Python whilst preserving order?
Algorithm - How to delete duplicate elements in a list efficiently?

我已经阅读了很多从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)本身将为 O(n)列表必须搜索值 x 。整个运行时间是 O(n ^ 2)

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.

更有效( O(n))可能是:

result = []
seen = set()
for i in a:
    if i not in seen:
        result.append(i)
        seen.add(i)



如何在保留订单的同时从列表中删除重复项?

(顶部的答案还显示了如何在列表中执行此操作理解方式,这将比一个明确的循环更有效)

您可以轻松地自己编写代码使用 timeit [文档] 模块。例如,我将代码放在 func1 中,我的 func2 中。如果我用 1000 元素(不重复)的数组重复这个 1000 次:

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

现在有重复(只有100个不同的值):

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

这篇关于Python列表:这是删除重复同时保留顺序的最佳方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆