加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 22:36:20 所属栏目:Python 来源:网络整理
导读:本篇章节讲解python过滤字符串中不属于指定集合中字符的类。供大家参考研究。具体如下: # -*- coding: utf-8 -*-import setsclass Keeper(object): def __init__(self,keep): self.keep = sets.Set(map(ord,keep)) def __getitem__(self,n): if n n

本篇章节讲解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程序设计有所帮助。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读