python过滤字符串中不属于指定集合中字符的类实例
2015-06-30 00:00
806 查看
本文实例讲述了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中项的方法
- python简单判断序列是否为空的方法
- python实现爬取千万淘宝商品的方法
- Python实现监控程序执行时间并将其写入日志的方法
- python的keyword模块用法实例分析
- Python项目一:即时标记
- Python中super关键字的作用
- 深刻理解Python中的元类(metaclass)
- Python的getattr(),setattr(),delattr(),hasattr()
- Python 闭包
- 关于Python的super用法
- [Python] Python 之 __new__() 方法与实例化
- [深入Python]__new__和__init__
- Python学习笔记 - 迭代器Iterator
- Python学习笔记 - 迭代器Iterator
- Python学习笔记 - 生成器generator
- Python学习笔记 - 生成器generator
- 列表元素的个数及元素的值可改变;元组值不可以改变-《Python核心编程》P26
- Python学习笔记 - 列表生成式listComprehensions