def remove_ignore_char(word): ignore_char = ['A', 'B', 'C', ] for char in ignore_char: if char in word: word = word.replace(char, '') return word
人生最曼妙的风景,竟是内心的淡定与从容!
from functools import reduce def remove_ignore_char(s): return reduce(lambda s, c: s.replace(c, ''), [s]+['A', 'B'])
Python 2 supprime la première ligne
ou
import re def remove_ignore_char(s): return re.sub('|'.join(['A', 'B']), '', s)
Cette méthode est la plus simple~ python3
>>> 'ABCxAxBxFCABxCabc'.translate(''.maketrans('','','ABC')) 'xxxFxabc'
import re def remove_ignore_char(word): return re.sub(r'[ABC]', '', word)
你要的解决方案 import string table = string.maketrans("abcd", " ") word = "abcdefg" word.translate(table) 将上面的代码封装成一个非常通用的函数 def translator(frm="", to="", delete="", keep=None): if len(to) == 1: to = to * len(frm) trans = string.maketrans(frm, to) if keep is not None: allchars = string.maketrans("", "") delete = allchars.translate(allchars, keep.translate(allchars, delete)) def translate(s): return s.translate(trans, delete) return translate abcd_to_empty = translator(frm="abcd", to=" ") abcd_to_empty(word) delete_vowels = translator(delete="aoieu") delete_vowels(word) del_digits = translator(delete="1234567890") del_digits("abcdeeeee12223444dkfljadl")
my $a = "This is aA TestABC" $a.subst(/[ABC]/, '', :g)
Python 2 supprime la première ligne
ou
Cette méthode est la plus simple~
python3