Python filters classes of characters in strings that do not belong to the specified collection

From , 3 Years ago, written in Python, viewed 79 times.
URL https://pastebin.vip/view/97de7621
  1. # -*- coding: utf-8 -*-
  2. import sets
  3. class Keeper(object):
  4.     def __init__(self, keep):
  5.         self.keep = sets.Set(map(ord, keep))
  6.     def __getitem__(self, n):
  7.         if n not in self.keep:
  8.             return None
  9.         return unichr(n)
  10.     def __call__(self, s):
  11.         return s.translate(self)
  12. makefilter = Keeper
  13. if __name__ == '__main__':
  14.     just_vowels = makefilter('aeiouy')
  15.     print just_vowels(u'four score and seven years ago')
  16.     # 输出: ouoeaeeyeaao
  17.     print just_vowels(u'tiger, tiger burning bright')
  18.     # 输出: ieieuii
  19.  
  20. #//python/5061

Reply to "Python filters classes of characters in strings that do not belong to the specified collection"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website