Skip to content Skip to main navigation Skip to footer

python过滤字符串中不属于指定集合中字符的类

python过滤字符串中不属于指定集合中字符是如何来实现的呢?下面的内容将会通过具体的实例来演示python过滤字符串中不属于指定集合中字符的实现方法及相关技巧:

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

# -*- 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过滤字符串中不属于指定集合中字符就是这样,欢迎大家参考。。。。

0 Comments

There are no comments yet

Leave a comment

Your email address will not be published.