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

Python Cookbook学习记录 ch1_10_2013/10/23

2013-10-23 22:50 483 查看
1.10 过滤字符串中不属于指定集合的字符

这个函数的实现方法和上一节一样:"除去要保留的剩下的都是要删除的"

>>> import string
>>> allchars = string.maketrans('','')
>>> def makefilter(keep):
delchars = allchars.translate(allchars,keep)
def thefilters(s):
return s.translate(allchars,delchars)
return thefilters

>>> just_vowels = makefilter('aeiou')
>>> just_vowels('hello world')
'eoo'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: