1

可以更快地检查字符串中是否存在子字符串(在我的情况下是空格),然后再删除它,或者relace()全局使用,就像说话一样;

用例:

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

也欢迎任何其他建议。

4

2 回答 2

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 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   

注意不能 time str.translate,因为str.translate在 Python 3 中不接受deletechars参数。

于 2013-11-14T08:20:12.870 回答
2

如果您正在谈论仅删除空格,则可以使用translate.

a = ['452 04','45204','455 04','88804']
a = [item.translate(None, " ") for item in a]
print a
于 2013-11-14T08:16:13.007 回答