-5

注意:最大文字列は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 に答える