python过滤字符串中不属于指定集合中字符的类实例
本文实例讲述了python过滤字符串中不属于指定集合中字符的类。分享给大家供大家参考。具体如下:
#-*-coding:utf-8-*-
importsets
classKeeper(object):
def__init__(self,keep):
self.keep=sets.Set(map(ord,keep))
def__getitem__(self,n):
ifnnotinself.keep:
returnNone
returnunichr(n)
def__call__(self,s):
returns.translate(self)
makefilter=Keeper
if__name__=='__main__':
just_vowels=makefilter('aeiouy')
printjust_vowels(u'fourscoreandsevenyearsago')
#输出:ouoeaeeyeaao
printjust_vowels(u'tiger,tigerburningbright')
#输出:ieieuii
希望本文所述对大家的Python程序设计有所帮助。
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短