如果您使用的是python2并且您的输入是字符串(不是Unipre),则绝对最佳的方法是
str.translate:
>>> chars_to_remove = ['.', '!', '?']>>> subj = 'A.B!C?'>>> subj.translate(None, ''.join(chars_to_remove))'ABC'
否则,可以考虑以下选项:
A.通过char迭代主题char,省略不需要的字符和
join结果列表:
>>> sc = set(chars_to_remove)>>> ''.join([c for c in subj if c not in sc])'ABC'
(请注意,生成器版本的
''.join(c for c ...)效率较低)。
B.动态创建一个正则表达式,并
re.sub带有一个空字符串:
>>> import re>>> rx = '[' + re.escape(''.join(chars_to_remove)) + ']'>>> re.sub(rx, '', subj)'ABC'
(
re.escape确保字符喜欢
^或
]不会破坏正则表达式)。
C.使用以下映射的变体
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'
完整的测试代码和时间安排:
#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_unipre(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_unipre(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_unipre)print ('nunipre 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_unipre)else: profile(remove_chars_re)profile(remove_chars_translate_unipre)
结果:
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_bytesunipre string:0.866 remove_chars_iter0.680 remove_chars_re_unipre1.373 remove_chars_translate_unipre---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_unipreunipre string:0.817 remove_chars_iter0.686 remove_chars_re0.876 remove_chars_translate_unipre
(作为一个旁注,该数字
remove_chars_translate_bytes可能为我们提供了一个线索,说明为什么该行业这么长时间不愿采用Unipre)。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)