Python Cookbook学习记录 ch1_10_2013/10/23

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'

 

posted on 2013-10-23 22:50  七海之风  阅读(112)  评论(0)    收藏  举报

导航