in operator for strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Moosebumps

    in operator for strings

    Wouldn't it be cool if you could do this?

    ('hello','test' ) in 'blah blah hello blah test':

    and it would say if all elements of the list are in the string?

    it would be more efficient and more readable than:

    'hello' in 'blah blah hello blah test' and 'test' in 'blah blah hello blah
    test'


  • Heather Coppersmith

    #2
    Re: in operator for strings

    On Tue, 25 May 2004 08:56:19 GMT,
    "Moosebumps " <moosebumps@moo sebumps.mb> wrote:
    [color=blue]
    > Wouldn't it be cool if you could do this?
    > ('hello','test' ) in 'blah blah hello blah test':[/color]
    [color=blue]
    > and it would say if all elements of the list are in the string?[/color]
    [color=blue]
    > it would be more efficient and more readable than:[/color]
    [color=blue]
    > 'hello' in 'blah blah hello blah test' and 'test' in 'blah blah hello blah
    > test'[/color]

    Untested:

    def string_contains _any_of( targets, the_string ):
    for target in targets:
    if target in the_string:
    return True
    return False

    string_contains _any_of( ('hello', 'test'), 'blah blah blah' )

    HTH,
    Heather

    --
    Heather Coppersmith
    That's not right; that's not even wrong. -- Wolfgang Pauli

    Comment

    • Fredrik Lundh

      #3
      Re: in operator for strings

      "Moosebumps " wrote:
      [color=blue]
      > Wouldn't it be cool if you could do this?
      >
      > ('hello','test' ) in 'blah blah hello blah test':
      >
      > and it would say if all elements of the list are in the string?[/color]

      well, if you're asking me, I'd say that what's really cool is that you
      can do not only

      findall(('hello ', 'test'), 'blah blah hello blah test')

      but also

      find_any(('hell o', 'test'), 'blah blah hello blah test')

      find_in_order(( 'hello', 'test'), 'blah blah hello blah test')

      find_in_any_cas e(('hello', 'test'), 'blah blah hello blah test')

      starts_and_ends _with(('hello', 'test'), 'blah blah hello blah test')

      find_only_on_fr idays(('hello', 'test'), 'blah blah hello blah test')

      etc.

      without having to change the language, or any of its implementations .

      </F>




      Comment

      • Larry Bates

        #4
        Re: in operator for strings

        This can be written as:

        a=('hello','tes t')
        b='blah blah hello blah test'
        reduce(lambda x,y: x and b.count(y), a)

        or as a fuction:

        def contains_all(a, b):
        return reduce(lambda x,y: x and b.count(y), a)


        Then in main program

        if contains_all(a, b):
        ...

        Larry Bates
        Syscon, Inc.

        "Moosebumps " <moosebumps@moo sebumps.mb> wrote in message
        news:TSDsc.7271 4$pN7.62390@new ssvr25.news.pro digy.com...[color=blue]
        > Wouldn't it be cool if you could do this?
        >
        > ('hello','test' ) in 'blah blah hello blah test':
        >
        > and it would say if all elements of the list are in the string?
        >
        > it would be more efficient and more readable than:
        >
        > 'hello' in 'blah blah hello blah test' and 'test' in 'blah blah hello blah
        > test'
        >
        >[/color]


        Comment

        Working...