python bloomfilter 布隆过滤器

本文使用的是pybloom_live包
git地址 https://github.com/joseph-fox/python-bloomfilter

import os
from pybloom_live import BloomFilter
animals = ['dog', 'cat', 'giraffe', 'fly', 'mosquito', 'horse', 'eagle',
           'bird', 'bison', 'boar', 'butterfly', 'ant', 'anaconda', 'bear',
           'chicken', 'dolphin', 'donkey', 'crow', 'crocodile','testadd']

is_exist = os.path.exists('test.blm')
#判断是否存在bloom文件
#判断存在就读取
if is_exist:
    bf = BloomFilter.fromfile(open('test.blm', 'rb'))
   #没有该文件则创建bf对象 最后的时候保存文件
else:
    bf = BloomFilter(20000, 0.001)


for animal in animals:
    if animal in bf:
        print('pass')
        pass
    else:
        print('add one')
        bf.add(animal)
        bf.tofile(open('test.blm','wb'))

“`

这里写图片描述
添加了一个 新的字段后
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_33042187/article/details/78928192