简体   繁体   English

如何轻松检查字符串是否包含不应包含在其中的字符?

[英]How do I easily check whether a string contains a character that should not be in there?

I want to check if a string contains a character in this string 我想检查一个字符串是否在此字符串中包含一个字符

invalid = 'gGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ'

I am doing this naively using a for loop: 我正在天真地使用for循环:

for ch in invalid:
    if ch in snippet:
        return False

This works, but I wonder if there is a more efficient or elegant way of doing this. 这行得通,但是我想知道是否有更有效或更优雅的方法来做到这一点。

This is quite efficient already, you might want to look at re.search which is optimised and can search all your char in one pass. 这已经非常有效了,您可能需要查看经过优化的re.search ,并且可以一次搜索所有字符。

I would also suggest this which is really really fast and does it in one pass : 我还建议这样做,这确实非常快,并且可以一口气完成它:

invalid = set('gGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ')
for i in snippet:
    if i in invalid:
        return False

You can go full set if you want but I doubt this will be faster due to conversion time : 您可以根据需要进行全套设置,但是我怀疑由于转换时间会更快:

invalid = set('gGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ')
if set(snippet) & invalid:
    return False

If it is this string in particular, it can be optimised further: 如果特别是此字符串,则可以进一步优化:

for i in snippet:
    if 'g' <= i <= 'z' or 'G' <= i <= 'Z':
        return False

Searching a set with in is the most efficient way, after comes dicts. 发出指令后,用in搜索集合是最有效的方法。

my_set = set(list(invalid))

example

'o' in my_set

you can set to get the common characters in both the strings and that tells you which characters are common in both of them. 您可以设置为获取两个字符串中的公共字符,并告诉您两个字符中的哪些公共字符。 by doing so you can capture all the characters that are invalid in your string. 这样,您可以捕获字符串中所有无效的字符。

set(snippet) & set(invalid) 设置(摘要)和设置(无效)

you can use sets like this 您可以使用这样的集合

invalid = 'gGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ'

snippet = 'gJ'
# this will return True because characters in "snippet" are also present in "invalid"
bool(set(invalid).intersection(set(snippet)))

snippet = 'a'
# this will return False because characters in "snippet2" are not there in "invalid"
bool(set(invalid).intersection(set(snippet)))

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

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