BloomFilter written in Python

Source: Internet
Author: User

Because Python does not have a built-in bitset data structure, but BitVector needs to be installed on its own, it is very convenient to install BitVector in the same way as installing a third-party module in Python: After the command line enters the directory, enter python setup. py install is an experiment on windows. It is a bit confusing to use BitVector in that directory after installation... the following code is rewritten based on java's BloomFilter: [python] # _ * _ coding: utf_8 _ import BitVector import OS import sys class SimpleHash (): def _ init _ (self, cap, seed): self. cap = cap self. seed = seed def hash (self, value): ret = 0 for I in range (len (value): ret + = self. seed * ret + ord (value [I]) return (self. cap-1) & ret class BloomFilter (): def _ init _ (self, BIT_SIZE = 1 <25): self. BIT_SIZE = 1 <25 self. seeds = [5, 7, 11, 13, 31, 37, 61] self. bitset = BitVector. bitVector (size = self. BIT_SIZE) self. hashFunc = [] for I in range (len (self. seeds): self. hashFunc. append (SimpleHash (self. BIT_SIZE, self. seeds [I]) def insert (self, value): for f in self. hashFunc: loc = f. hash (value) self. bitset [loc] = 1 def isContaions (self, value): if value = None: return False ret = True for f in self. hashFunc: loc = f. hash (value) ret = ret & self. bitset [loc] return ret def main (): fd = open ("urls.txt") bloomfilter = BloomFilter () while True: # url = raw_input () url = fd. readline () if cmp (url, 'exit ') = 0: # if url is equal exit break if bloomfilter. isContaions (url) = False: bloomfilter. insert (url) else: print 'url: % s has exist '% url main ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.