简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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