
steve at REMOVE-THIS-cybersource
Jul 17, 2009, 1:06 AM
Post #13 of 13
(486 views)
Permalink
|
|
Re: How to check if any item from a list of strings is in a big string?
[In reply to]
|
|
On Thu, 16 Jul 2009 11:02:57 -0500, Pablo Torres N. wrote: > On Thu, Jul 9, 2009 at 22:07, Steven > D'Aprano<steven [at] remove> wrote: >> 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. >> >> > Why reverse the order of the arguments? Is there a design principle > there? It's just a convention. Before strings had methods, you used the string module, e.g.: string.find(source, target) => find target in source This became source.find(target). In your function: list_items_in_string(list_items, string) "list_items" is equivalent to target, and "string" is equivalent to source. It's conventional to write the source first. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
|