[英]How to check if string contains chars which are not in a list?
我有个问题。 如何检查python字符串是否包含不在给定列表中的字符?
这是列表(设置):
set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._")
在验证字符串时,我总是遵循正则表达式。
要创建一个集合,请将集合中的所有字符括在[]
。 要检查字符串是否包含不在集合中的任何字符,请在开头添加^
。 要检查字符串是否包含一组的一个或多个成员,请附加+
。
鉴于此信息,用于检查字符串是否包含除{a,b,c,d}之外的任何字符的正则表达式如下所示:
[^abcd]+
(注意这是区分大小写的)
要在 python 中使用正则表达式,请import re
。 re.search(pattern, string, flags=0)
方法将在整个字符串中查找您提供的模式。
您想测试字符串中的字符是否不是给定字符集的子集。 这在 Python 中很简单,因为<=
运算符测试一个集合是否是另一个集合的子集。
import string
# don't use a mutable set for this purpose
GIVEN = frozenset(string.ascii_letters + string.digits + '-._')
def uses_other_chars(s, given=GIVEN):
return not set(s) <= given
例子:
>>> uses_other_chars('abc')
False
>>> uses_other_chars('Hello!')
True
any
为字符串的每个字符检入 SETSET = set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._")
s = "123#"
print(any(x not in SET for x in s))
比较不同解决方案的运行时间:
import timeit
search_strings = [
'"#12"', # short string, early match
'"#1234567"', # longer string, early match
'"1234567#"', # longer string, late match
'"123" * 100 + "#"', # long string, late match
'"123#" * 100', # long string early match
]
algorithms = [
("r.search(s)", 's={};import re; r = re.compile(r"[^-.\w]")'),
("set(s) - SET", 's={};SET=frozenset("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._")'),
("any(x not in SET for x in s)", 's={};SET=frozenset("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._")'),
("SET.issuperset(s)", 's={};SET=frozenset("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._")'),
]
for alg, setup in algorithms:
print alg
for sstr in search_strings:
print "%35s %.3f" % (sstr[:35], timeit.timeit(alg, setup.format(sstr)))
它在我的机器上提供以下输出:
r.search(s)
"#12" 0.470
"#1234567" 0.514
"1234567#" 0.572
"123" * 100 + "#" 3.493
"123#" * 100 0.502
set(s) - SET
"#12" 0.566
"#1234567" 1.045
"1234567#" 1.075
"123" * 100 + "#" 7.658
"123#" * 100 10.170
any(x not in SET for x in s)
"#12" 0.786
"#1234567" 0.797
"1234567#" 1.475
"123" * 100 + "#" 27.266
"123#" * 100 1.087
SET.issuperset(s)
"#12" 0.466
"#1234567" 0.864
"1234567#" 0.896
"123" * 100 + "#" 7.512
"123#" * 100 10.199
我们看到正则表达式解决方案是最快的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.