asking

Tim Chase python.list at tim.thechases.com
Wed Aug 22 06:59:01 EDT 2012


On 08/22/12 04:42, alex23 wrote:
> On 08/22/2012 03:17 AM, mingqiang hu wrote:
>> I mean any of "a","b","c" in string "adfbdfc"  makes the statement true,can
>> I not use a function?
> 
> any(map(string.__contains__, substrings))

As map()/reduce() vs. list-comprehension discussions are going on in
another thread, I find it more readable to write

  any(letter in "abfbdfc" for letter in "abc")

or

  all(letter in "abfbdfc" for letter in "abc")

depending on what the OP wants (a matter clarity which the OP seems
to be a little vague on).

Alternatively, if they're actual sub-strings rather than just
letters, it can be written as

  substrings = [
    "abf",
    "bfb",
    "fbd",
    ]
  target = "abfdbfc"
  any(substring in target for substring in substrings)
  all(substring in target for substring in substrings)

which is about as close to English as it gets.

If the target is exceptionally long, I'd be tempted to go with
Terry's suggestion of a regular expression which should be able to
do the checks in one search pass of the target haystack.

-tkc









More information about the Python-list mailing list