联合集或检查整个列表是否更快? [英] Is it faster to union sets or check the whole list for a duplicate?

查看:55
本文介绍了联合集或检查整个列表是否更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,标题措辞不当,但我之前问过一个关于从两个列表中获取唯一项目列表的问题.人们告诉我先列出清单 -> 集合,然后联合.

所以现在我想知道是否更快:

  1. 向列表添加一项项时,扫描整个列表是否有重复项.
  2. 先将一个项目设为集合,然后再将集合设为联合集.

事后看来,我可能应该只阅读片场...

顺便说一下,在 Python 中 - 抱歉没有澄清.

解决方案

如你所见,将一个列表扩展到另一端,然后通过设置删除重复项是最快的方式(至少在python中;))

<预><代码>>>>定义 foo():……"...从另一端扩展一个列表,然后通过设置删除重复项……"... l1 = 范围(200)... l2 = 范围(150, 250)... l1.extend(l2)...设置(l1)...>>>定义栏():……"...检查元素是否在一个列表中,只有在没有时才添加它……"... l1 = 范围(200)... l2 = 范围(150, 250)...对于 l2 中的元素:...如果 elem 不在 l1 中:... l1.append(elem)...>>>def baz():……"...从两个列表中创建集合,然后将它们并集……"... l1 = 范围(200)... l2 = 范围(150, 250)...设置(l1)|设置(l2)...>>>从 timeit 导入计时器>>>计时器(foo).timeit(10000)0.265153169631958>>>计时器(bar).timeit(10000)7.921358108520508>>>计时器(baz).timeit(10000)0.3845551013946533>>>

Sorry for the poorly worded title but I asked a question earlier about getting a unique list of items from two lists. People told me to make the list -> sets and then union.

So now I'm wondering if it's faster to:

  1. While adding one item to a list, scan the whole list for duplicates.
  2. Make that one item a set and then union sets.

I should probably just read up on sets in hindsight...

In Python, by the way - sorry for not clarifying.

解决方案

as you can see extending one list by another end then remove duplicates by making set is the fastest way(at least in python;))

>>> def foo():
...     """
...     extending one list by another end then remove duplicates by making set
...     """
...     l1 = range(200)
...     l2 = range(150, 250)
...     l1.extend(l2)
...     set(l1)
... 
>>> def bar():
...     """
...     checking if element is on one list end adding it only if not
...     """
...     l1 = range(200)
...     l2 = range(150, 250)
...     for elem in l2:
...             if elem not in l1:
...                     l1.append(elem)
... 
>>> def baz():
...     """
...     making sets from both lists and then union from them
...     """
...     l1 = range(200)
...     l2 = range(150, 250)
...     set(l1) | set(l2)
... 
>>> from timeit import Timer
>>> Timer(foo).timeit(10000)
0.265153169631958
>>> Timer(bar).timeit(10000)
7.921358108520508
>>> Timer(baz).timeit(10000)
0.3845551013946533
>>> 

这篇关于联合集或检查整个列表是否更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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