BloomFilter布隆过滤器python的实现方法

BloomFilter布隆过滤器是什么?

不知道BloomFilter的朋友,请参考这篇文章海量数据处理算法—Bloom Filter

BloomFilter布隆过滤器python的实现方法

  1. 安装pybloomfiltermmap模块
    pip install pybloomfiltermmap
  2. 实现
     
    # -*- coding:utf-8 -*-  
     
    import os  
    import sys  
    reload(sys)  
    sys.setdefaultencoding('utf-8')  
     
    import random  
     
    from pybloomfilter import BloomFilter  
     
    # 申请一个内存等于1M的容器,错误率等于0.001的bloomfilter对象  
    bfilter = BloomFilter(1*1024*1024,0.001)  
     
    # 添加100万个元素  
    for x in xrange(1000000):  
        bfilter.add(str(x))  
     
    # 测试错误率
    error_in = 0  
    for x in xrange(2000000):  
        if str(x) in bfilter and x > 1000000:  
            error_in += 1  
     
    print "error_rate:%s" % (error_in*1.0/1000000)

参考

https://axiak.github.io/pybloomfiltermmap/
http://blog.csdn.net/wh_springer/article/details/52193110

Leave a Reply

Your email address will not be published. Required fields are marked *