您的位置:首页 > 编程语言 > Python开发

python过滤字符串中不属于指定集合中字符的类实例

2015-06-30 17:18 639 查看

本文实例讲述了python过滤字符串中不属于指定集合中字符的类。分享给大家供大家参考。具体如下:

# -*- coding: utf-8 -*-
import sets
class Keeper(object):
def __init__(self, keep):
self.keep = sets.Set(map(ord, keep))
def __getitem__(self, n):
if n not in self.keep:
return None
return unichr(n)
def __call__(self, s):
return s.translate(self)
makefilter = Keeper
if __name__ == '__main__':
just_vowels = makefilter('aeiouy')
print just_vowels(u'four score and seven years ago')
# 输出: ouoeaeeyeaao
print just_vowels(u'tiger, tiger burning bright')
# 输出: ieieuii

希望本文所述对大家的Python程序设计有所帮助。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 过滤 字符串