在删除子字符串之前检查子字符串是否更快?

时间:2013-11-14 08:13:49

标签: python replace

在删除字符串之前检查子字符串(在我的情况下是一个空格)是否更快,或者在全局使用relace()时,是否更快;

用例:

a = ['452 04','45204','455 04','88804']
for i,e in enumerate(a):
    if re.search(r"\s", e):
       a[i] = e.replace(' ','')

也欢迎任何其他建议。

2 个答案:

答案 0 :(得分:10)

import re

def with_re_search():
    a = ['452 04','45204','455 04','88804']
    for i,e in enumerate(a):
        if re.search(r"\s", e):
            a[i] = e.replace(' ','')

def with_in():
    a = ['452 04','45204','455 04','88804']
    for i,e in enumerate(a):
        if ' ' in e:
            a[i] = e.replace(' ','')

def without_search():
    a = ['452 04','45204','455 04','88804']
    for i,e in enumerate(a):
        a[i] = e.replace(' ','')

def with_translate():
    a = ['452 04','45204','455 04','88804']
    for i, e in enumerate(a):
        a[i] = e.translate(None,'')

from timeit import timeit as t
n = 1000000
t('f()', setup='from __main__ import with_re_search as f', number=n) # 5.37417006493
t('f()', setup='from __main__ import with_in as f',        number=n) # 1.04646992683
t('f()', setup='from __main__ import without_search as f', number=n) # 1.2475438118
t('f()', setup='from __main__ import with_translate as f', number=n) # 1.56214690208

使用re.search绝对比其他选项慢。

这是在CPython 2.7.3,Ubuntu Linux 12.10 64bit中完成的。

UPDATE :在CPython 3.3.0(同一台机器)中。

t('f()', setup='from __main__ import with_re_search as f', number=n) # 24.345079875027295
t('f()', setup='from __main__ import with_in as f',        number=n) # 1.1399168980424292
t('f()', setup='from __main__ import without_search as f', number=n) # 1.3967725560069084   

注意无法计时str.translate,因为Python 3中的str.translate不接受deletechars参数。

答案 1 :(得分:2)

如果您所说的只是删除空格,可以使用translate

a = ['452 04','45204','455 04','88804']
a = [item.translate(None, " ") for item in a]
print a
相关问题