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'
浙公网安备 33010602011771号