html5中文学习网

您的位置: 首页 > 网站及特效实例 > jquery特效 » 正文

python过滤字符串中不属于指定集合中字符的类实例_编程语言综合

[ ] 已经帮助:人解决问题

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

  ?FSgHTML5中文学习网 - HTML5先行者学习网

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# -*- 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程序设计有所帮助。FSgHTML5中文学习网 - HTML5先行者学习网

(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助