简体   繁体   English

如何检查字符串是否包含不在列表中的字符?

[英]How to check if string contains chars which are not in a list?

I have a question.我有个问题。 How do I check if a python string contains chars which are not in a given list?如何检查python字符串是否包含不在给定列表中的字符?

Here is the list (set):这是列表(设置):

set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._")

I always defer to regular expressions when validating strings.在验证字符串时,我总是遵循正则表达式。

To create a set, you enclose all characters in the set in [] .要创建一个集合,请将集合中的所有字符括在[] To check if a string contains any character not in a set, add ^ to the beginning.要检查字符串是否包含不在集合中的任何字符,请在开头添加^ To check if the string contains one or more members of a set, append + .要检查字符串是否包含一组的一个或多个成员,请附加+

Given this information, a regular expression to check if a string contains any characters other than {a,b,c,d} would look like this:鉴于此信息,用于检查字符串是否包含{a,b,c,d}之外的任何字符的正则表达式如下所示:

[^abcd]+ (note that this is case sensitive) [^abcd]+ (注意这是区分大小写的)

To use regular expressions in python, import re .要在 python 中使用正则表达式,请import re The re.search(pattern, string, flags=0) method will look through the entire string for the pattern you give. re.search(pattern, string, flags=0)方法将在整个字符串中查找您提供的模式。

More information on regular expressions in python can be found here .可以在此处找到有关 Python 中正则表达式的更多信息。 A simple regular expression tester can be found here .一个简单的正则表达式测试器可以在这里找到。

You want to test whether the characters in the string are not a subset of the given set of characters.您想测试字符串中的字符是否不是给定字符集的子集 That is straightforward in Python because the <= operator tests if one set is a subset of another.这在 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

Examples:例子:

>>> uses_other_chars('abc')
False
>>> uses_other_chars('Hello!')
True
  • use any to check in SET for each character of string使用any为字符串的每个字符检入 SET
SET = set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._")
s = "123#"
print(any(x not in SET for x in s))

Comparing the runtime of the different solutions:比较不同解决方案的运行时间:

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

which gives the following output on my machine:它在我的机器上提供以下输出:

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

we see that the regex solution is the fastest.我们看到正则表达式解决方案是最快的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM