#!coding=utf-8def containsAny(seq,aset): """ 检查序列seq是否含有aset中的项 """ for item in seq: if item in aset:return True return Falseimport itertoolsdef containsAnyItertools(seq,aset): """ 性能好点,但是本质和contansAny一样 """ for item in itertools.ifilter(aset.__contains__,seq): return True return Falsedef containsAnySet(seq,aset): """ seq中的每个元素都要检测,没有短路特性 """ return bool(set(aset).intersection(seq))def containsAllSet(seq,aset): """ 检测seq中是否包含aset的所有项 """ #相当于aset - seq,返回属于aset但是不输入seq的元素.不要和symmetric_difference搞混, # symmetric_difference是属于一个期中一个序列,但是不属于both的元素集合,a^b return not set(aset).difference(seq)import stringnotrans = string.maketrans('','') #不做转换,可以将第一个参数转换为第二个参数def containsAnyStr(seq,aset): return len(aset) != len(aset.translate(notrans,seq))def containsAllStr(seq,aset): return not aset.translate(notrans,seq) # 利用translate将aset中的与seq的项相同的项删掉def translator(fm='',to='',delete='',keep=None): """ 简化tring.translate的使用 translate 比set方式快,但是不使用unicode """ if len(to) == 1:to = to*len(fm) trans = string.maketrans(fm,to) if keep != None: allchars = string.maketrans('','') #返回经过转换后的256个可打印字符组成的string delete = allchars.translate(allchars,keep.translate(allchars,delete)) #得到根据需要保留的keep计算后需要删除的项 def translate(s): return s.translate(trans,delete) return translateclass Keeper(object): """ For Unicode objects, the translate() method does not accept the optional deletechars argument. Instead, it returns a copy of the s where all characters have been mapped through the given translation table which must be a mapping of Unicode ordinals to Unicode ordinals, Unicode strings or None. Unmapped characters are left untouched. Characters mapped to None are deleted """ def __init__(self,keep): self.keep = set(map(ord,keep)) def __getitem__(self, item): #对该类的对象执行索引的时候调用 if item not in self.keep: return None return unichr(item) def __call__(self, s): return unicode(s).translate(self)