How to check if any item from a list of strings is in a big string?
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Thu Jul 9 23:07:09 EDT 2009
On Thu, 09 Jul 2009 18:36:05 -0700, inkhorn wrote:
> def list_items_in_string(list_items, string):
> for item in list_items:
> if item in string:
> return True
> return False
...
> Any ideas how to make that function look nicer? :)
Change the names. Reverse the order of the arguments. Add a docstring.
Otherwise looks pretty nice to me. Simple, straightforward, and correct.
If you're running Python 2.5 or better, then this is even shorter (and
probably faster):
def contains(s, targets):
"""Return True if any item of targets is in string s."""
return any(target in s for target in targets)
--
Steven
More information about the Python-list
mailing list