与set()相比,Python中的成员资格测试更快 [英] faster membership testing in python than set()

查看:114
本文介绍了与set()相比,Python中的成员资格测试更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须检查包含10-100k个元素的列表中是否存在数百万个元素(str的20-30个字母).在python中是否有比set()更快的方法?

I have to check presence of millions of elements (20-30 letters str) in the list containing 10-100k of those elements. Is there faster way of doing that in python than set() ?

import sys
#load ids
ids = set( x.strip() for x in open(idfile) )

for line in sys.stdin:
    id=line.strip()
    if id in ids:
        #print fastq
        print id
        #update ids
        ids.remove( id )

推荐答案

set尽可能快.

但是,如果您重写代码以一次创建set而不更改它,则可以使用frozenset内置类型.除了不可变,它完全相同.

However, if you rewrite your code to create the set once, and not change it, you can use the frozenset built-in type. It's exactly the same except immutable.

如果您仍然遇到速度问题,则需要以其他方式加速程序,例如使用 PyPy 而不是cPython

If you're still having speed problems, you need to speed your program up in other ways, such as by using PyPy instead of cPython.

这篇关于与set()相比,Python中的成员资格测试更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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