简体   繁体   中英

How to check that none of the chars are in the string?

def lookup(content):
    value=True
    if any(x not in content for x in ['A','B','C']):
        value = False
    print (value)

What i want is check if any of these 'A' 'B' 'C' is in a string , for example if string equates ABCAA then the value will be true, if the string is ABDC then value is wrong because content contains a char not defined in my list above.The issue is i'm getting false with that function for 'ABC' which isn't supposed to happen.

Just use sets:

# input
my_string = 'abcd'

# set of allowed characters
approved_characters = set('abc')

# characters in string that are not in set of approved characters
unapproved_characters = set(my_string) - approved_characters # gives {'d'}

Try this:

def lookup(content):
    print(all(x in ['A','B','C'] for x in content))

lookup('ABC') #output: True

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