-5

注意:最大字符串 ling 将为 15 个字符。

4

2 回答 2

4

为了比较原始速度,这里是@wim 答案的比较,它的一些优化和一个正则表达式解决方案:

import re, string, timeit

clean = re.compile(r'[^a-zA-Z0-9_-]')
keep = string.ascii_letters + string.digits + '_-'
keep_set = set(string.ascii_letters + string.digits + '_-')
test = '$pam and_Eggs##-!'
cur_encoding_bytes = 256 # 8 for UTF-8, in Python 3 this would be different for Unicode
all_else = ''.join(chr(i) for i in range(cur_encoding_bytes) if chr(i) not in keep_set) # taken from https://stackoverflow.com/a/3588485/1219006

def clean1(s):
    return ''.join(x for x in s if x in keep)

def clean1_filter(s):
    return filter(keep.__contains__, s)

def clean1_filter_set(s):
    return filter(keep_set.__contains__, s)

def clean2(s):
    return clean.sub('', s)

def clean3(s):
    return s.translate(None, all_else)

print timeit.timeit('clean1(test)', 'from __main__ import clean1, test')
print timeit.timeit('clean1_filter(test)', 'from __main__ import clean1_filter, test')
print timeit.timeit('clean1_filter_set(test)', 'from __main__ import clean1_filter_set, test')
print timeit.timeit('clean2(test)', 'from __main__ import clean2, test')
print timeit.timeit('clean3(test)', 'from __main__ import clean3, test')

这个脚本的输出是:

2.96962522809
1.56208783165
1.10597814849
1.59298783663
0.53834820236

str.translate是最快的,是一种专门的字符串方法。

于 2012-10-06T02:28:13.320 回答
2
>>> def clean(s):
...   from string import ascii_letters, digits
...   keep = ascii_letters + digits + '_-'
...   return ''.join(x for x in s if x in keep)
... 
>>> clean('$pam and_Eggs##-!')
'pamand_Eggs-'
于 2012-10-06T02:33:41.843 回答