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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

不吃皮蛋   2021-03-21 我要评论
想了解python过滤字符串中不属于指定集合中字符的类实例的相关内容吗,不吃皮蛋在本文为您仔细讲解python过滤字符串中不属于指定集合中字符的类的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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程序设计有所帮助。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们