Skip to main content
 首页 » 编程设计

python之在删除子字符串之前检查它是否存在会更快吗

2025年05月04日19lautakyan007

在删除字符串之前检查字符串中是否存在子字符串(在我的例子中是空格),或者说全局使用 relace() 会更快;

用例:

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

也欢迎任何其他建议。

请您参考如下方法:

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 64 位中完成的。

更新:在 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 计时,因为 str.translate在 Python 3 中不接受 deletechars 参数。