[Python-ideas] contains_any_in and contains_all_in

Terry Reedy tjreedy at udel.edu
Wed Apr 24 00:17:45 EDT 2019

On 4/23/2019 4:39 PM, João Matos wrote:
> Hello,
> If we want to check if a string contains any/all of several other 
> strings we have to use several or/and conditions or any/all.
> For any:
> |if ('string1' in master_string or 'string2' in master_string
>          or 'string3' in master_string):
> or
> ifany(item inmaster_string foritem in['string1','string2','string3']):

Trivial with re module, which will answer the question in one pass.

> For all:
> |
> ||if ('string1' in master_string and 'string2' in master_string
>          and'string3' in master_string):
> or
> ||ifall(item inmaster_string foritem in['string1','string2','string3']):

Are the strings guaranteed to not be prefixes of each other?
Do you want to allow overlaps?
Can do in one pass by compiling a new re every time an item is found.
If overlaps not wanted, re.iterfind will find all occurrence of any, so 
feed to set and see if all found.

Terry Jan Reedy

More information about the Python-ideas mailing list