使用Python从字符串中剥离所有非字母数字字符的最佳方法是什么?
在这个问题的PHP变体中提出的解决方案可能会进行一些小的调整,但对我来说似乎不太“python化”。
声明一下,我不只是想去掉句号和逗号(以及其他标点符号),还想去掉引号、括号等。
使用Python从字符串中剥离所有非字母数字字符的最佳方法是什么?
在这个问题的PHP变体中提出的解决方案可能会进行一些小的调整,但对我来说似乎不太“python化”。
声明一下,我不只是想去掉句号和逗号(以及其他标点符号),还想去掉引号、括号等。
当前回答
我用perfplot(我的一个项目)检查了结果,发现对于短字符串,
"".join(filter(str.isalnum, s))
是最快的。对于长字符串(200+字符)
re.sub("[\W_]", "", s)
是最快的。
代码重现情节:
import perfplot
import random
import re
import string
pattern = re.compile("[\W_]+")
def setup(n):
return "".join(random.choices(string.ascii_letters + string.digits, k=n))
def string_alphanum(s):
return "".join(ch for ch in s if ch.isalnum())
def filter_str(s):
return "".join(filter(str.isalnum, s))
def re_sub1(s):
return re.sub("[\W_]", "", s)
def re_sub2(s):
return re.sub("[\W_]+", "", s)
def re_sub3(s):
return pattern.sub("", s)
b = perfplot.bench(
setup=setup,
kernels=[string_alphanum, filter_str, re_sub1, re_sub2, re_sub3],
n_range=[2**k for k in range(10)],
)
b.save("out.png")
b.show()
其他回答
如果我理解正确,最简单的方法是使用正则表达式,因为它为您提供了很大的灵活性,但另一个简单的方法是使用循环以下是示例代码,我还计算了单词的出现并存储在字典中。
s = """An... essay is, generally, a piece of writing that gives the author's own
argument — but the definition is vague,
overlapping with those of a paper, an article, a pamphlet, and a short story. Essays
have traditionally been
sub-classified as formal and informal. Formal essays are characterized by "serious
purpose, dignity, logical
organization, length," whereas the informal essay is characterized by "the personal
element (self-revelation,
individual tastes and experiences, confidential manner), humor, graceful style,
rambling structure, unconventionality
or novelty of theme," etc.[1]"""
d = {} # creating empty dic
words = s.split() # spliting string and stroing in list
for word in words:
new_word = ''
for c in word:
if c.isalnum(): # checking if indiviual chr is alphanumeric or not
new_word = new_word + c
print(new_word, end=' ')
# if new_word not in d:
# d[new_word] = 1
# else:
# d[new_word] = d[new_word] +1
print(d)
如果这个答案是有用的,请评价这个!
正则表达式的拯救:
import re
re.sub(r'\W+', '', your_string)
根据Python定义'\W == [^a-zA-Z0-9_],它不包括所有数字、字母和_
如何:
def ExtractAlphanumeric(InputString):
from string import ascii_letters, digits
return "".join([ch for ch in InputString if ch in (ascii_letters + digits)])
如果InputString中的字符存在于ascii_letters和digits的组合字符串中,则可以使用列表推导来生成InputString中的字符列表。然后它将列表连接在一起,形成一个字符串。
Python 3
使用与@John Machin的答案相同的方法,但针对Python 3进行了更新:
更大的字符集 对翻译工作方式的轻微改变。
现在假定Python代码是用UTF-8编码的 (来源:PEP 3120)
这意味着包含你想要删除的所有字符的字符串会变得更大:
del_chars = ''.join(c for c in map(chr, range(1114111)) if not c.isalnum())
翻译方法现在需要使用一个翻译表,我们可以用maketrans()创建:
del_map = str.maketrans('', '', del_chars)
现在,像以前一样,任何你想要“捏碎”的字符串:
scrunched = s.translate(del_map)
使用来自@Joe Machin的最后一个计时例子,我们可以看到它仍然比re强一个数量级:
> python -mtimeit -s"d=''.join(c for c in map(chr,range(1114111)) if not c.isalnum());m=str.maketrans('','',d);s='foo-'*25" "s.translate(m)"
1000000 loops, best of 5: 255 nsec per loop
> python -mtimeit -s"import re;s='foo-'*25;r=re.compile(r'[\W_]+')" "r.sub('',s)"
50000 loops, best of 5: 4.8 usec per loop
>>> import re
>>> string = "Kl13@£$%[};'\""
>>> pattern = re.compile('\W')
>>> string = re.sub(pattern, '', string)
>>> print string
Kl13