Check to see if any character appears on line A?

I have a string, A = "abcdef"and a few characters "a", "f"and "m". I want a condition to make sure that none of the characters are displayed in A, i.e.

if a not in A and f not in A and m not in A:
    # do something

Is there a better way to do this? Thank!

+3
source share
2 answers

Settings are useful for this - see method isdisjoint():

Returns True if the collection has no elements in common with another. Sets do not intersect if and only if their intersection is an empty set.

new in version 2.6.

>>> a = "abcde"
>>> b = "ace"
>>> c = "xyz"
>>> set(a).isdisjoint(set(b))
False
>>> set(a).isdisjoint(set(c))
True

edit after comment

sets are still you friend. If I follow you better now, you want this (or something close to it):

, :

>>> a = set('abcde')
>>> b = set('ace')
>>> c = set('acx')

, :

>>> a.intersection(b) == b
True

- , :

>>> a.intersection(c) == c
False

, ?

+11
True in [i in 'abcdef' for i in 'afm']

True

True in [i in 'nopqrst' for i in 'afm']

False

0

All Articles