python过滤字符串中不属于指定集合中字符的类实例,python字符串
python过滤字符串中不属于指定集合中字符的类实例,python字符串
本文实例讲述了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判断一个集合是否包含了另外一个集合中所有项的
- python检查序列seq是否含有aset中项的方法,seqaset
- python简单判断序列是否为空的方法,python判断序列
- python实现爬取千万淘宝商品的方法,python淘宝商品
- Python实现监控程序执行时间并将其写入日志的方法,
- python的keyword模块用法实例分析,pythonkeyword
- python实现linux下使用xcopy的方法,pythonxcopy
- 分析用Python脚本关闭文件操作的机制,
- python实现搜索指定目录下文件及文件内搜索指定关键词
- python中getaddrinfo()基本用法实例分析,pythongetaddrinfo
评论关闭