Python: How to check a string for substrings from

2019-01-22 10:57发布

Possible Duplicate:
Check if multiple strings exist in another string

I can't seem to find an equivalent of code that functions like this anywhere for Python:

Basically, I'd like to check a string for substrings contained in a list.

1条回答
放我归山
2楼-- · 2019-01-22 11:46

Try this test:

any(substring in string for substring in substring_list)

It will return True if any of the substrings in substring_list is contained in string.

Note that there is a Python analogue of Marc Gravell's answer in the linked question:

from itertools import imap
any(imap(s.__contains__, substring_list))

Probably the above version using a generator expression is clearer though.

查看更多
登录 后发表回答