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 ()