Removing a list of characters in string
If you're using python2 and your inputs are strings (not unicodes), the absolutely best method is str.translate
:
>>> chars_to_remove = ['.', '!', '?']>>> subj = 'A.B!C?'>>> subj.translate(None, ''.join(chars_to_remove))'ABC'
Otherwise, there are following options to consider:
A. Iterate the subject char by char, omit unwanted characters and join
the resulting list:
>>> sc = set(chars_to_remove)>>> ''.join([c for c in subj if c not in sc])'ABC'
(Note that the generator version ''.join(c for c ...)
will be less efficient).
B. Create a regular expression on the fly and re.sub
with an empty string:
>>> import re>>> rx = '[' + re.escape(''.join(chars_to_remove)) + ']'>>> re.sub(rx, '', subj)'ABC'
(re.escape
ensures that characters like ^
or ]
won't break the regular expression).
C. Use the mapping variant of translate
:
>>> chars_to_remove = [u'δ', u'Γ', u'ж']>>> subj = u'AжBδCΓ'>>> dd = {ord(c):None for c in chars_to_remove}>>> subj.translate(dd)u'ABC'
Full testing code and timings:
#coding=utf8import redef remove_chars_iter(subj, chars): sc = set(chars) return ''.join([c for c in subj if c not in sc])def remove_chars_re(subj, chars): return re.sub('[' + re.escape(''.join(chars)) + ']', '', subj)def remove_chars_re_unicode(subj, chars): return re.sub(u'(?u)[' + re.escape(''.join(chars)) + ']', '', subj)def remove_chars_translate_bytes(subj, chars): return subj.translate(None, ''.join(chars))def remove_chars_translate_unicode(subj, chars): d = {ord(c):None for c in chars} return subj.translate(d)import timeit, sysdef profile(f): assert f(subj, chars_to_remove) == test t = timeit.timeit(lambda: f(subj, chars_to_remove), number=1000) print ('{0:.3f} {1}'.format(t, f.__name__))print (sys.version)PYTHON2 = sys.version_info[0] == 2print ('\n"plain" string:\n')chars_to_remove = ['.', '!', '?']subj = 'A.B!C?' * 1000test = 'ABC' * 1000profile(remove_chars_iter)profile(remove_chars_re)if PYTHON2: profile(remove_chars_translate_bytes)else: profile(remove_chars_translate_unicode)print ('\nunicode string:\n')if PYTHON2: chars_to_remove = [u'δ', u'Γ', u'ж'] subj = u'AжBδCΓ'else: chars_to_remove = ['δ', 'Γ', 'ж'] subj = 'AжBδCΓ'subj = subj * 1000test = 'ABC' * 1000profile(remove_chars_iter)if PYTHON2: profile(remove_chars_re_unicode)else: profile(remove_chars_re)profile(remove_chars_translate_unicode)
Results:
2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]"plain" string:0.637 remove_chars_iter0.649 remove_chars_re0.010 remove_chars_translate_bytesunicode string:0.866 remove_chars_iter0.680 remove_chars_re_unicode1.373 remove_chars_translate_unicode---3.4.2 (v3.4.2:ab2c023a9432, Oct 5 2014, 20:42:22) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]"plain" string:0.512 remove_chars_iter0.574 remove_chars_re0.765 remove_chars_translate_unicodeunicode string:0.817 remove_chars_iter0.686 remove_chars_re0.876 remove_chars_translate_unicode
(As a side note, the figure for remove_chars_translate_bytes
might give us a clue why the industry was reluctant to adopt Unicode for such a long time).
You can use str.translate()
:
s.translate(None, ",!.;")
Example:
>>> s = "asjo,fdjk;djaso,oio!kod.kjods;dkps">>> s.translate(None, ",!.;")'asjofdjkdjasooiokodkjodsdkps'