asking

Ian Foote ian at feete.org
Tue Aug 21 23:42:55 EDT 2012


On 22/08/12 03:57, mingqiang hu wrote:
> can I use just one statement to figure out if substring “a” ,"b" "c" 
> are in string "adfbdfc" ? not use the statement like
>
> ("a" in "adfbdfc") or ( "b" in "adfbdfc") or ("c" in "adfbdfc" ) 
> ,because if I have lots of substring, this could sucks

This might not be the most efficient way, but:

 >>> set("abc") <= set("adfbdfc")
True
 >>> set("abce") <= set("adfbdfc")
False

If you want to check for substrings longer than one character, this 
won't work. A solution then is to define a custom function:

def all_in(string, substrings):
for substring in substrings:
if substring not in string:
return False
return True

Ian



More information about the Python-list mailing list