in operator for strings

Discussion in 'Python' started by Moosebumps, May 25, 2004.

  1. Moosebumps

    Moosebumps Guest

    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'
     
    Moosebumps, May 25, 2004
    #1
    1. Advertising

  2. "Moosebumps" wrote:

    > 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?


    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(('hello', 'test'), 'blah blah hello blah test')

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

    find_in_any_case(('hello', 'test'), 'blah blah hello blah test')

    starts_and_ends_with(('hello', 'test'), 'blah blah hello blah test')

    find_only_on_fridays(('hello', 'test'), 'blah blah hello blah test')

    etc.

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

    </F>
     
    Fredrik Lundh, May 25, 2004
    #2
    1. Advertising

  3. On Tue, 25 May 2004 08:56:19 GMT,
    "Moosebumps" <> wrote:

    > 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'


    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
     
    Heather Coppersmith, May 25, 2004
    #3
  4. Moosebumps

    Larry Bates Guest

    This can be written as:

    a=('hello','test')
    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" <> wrote in message
    news:TSDsc.72714$...
    > 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'
    >
    >
     
    Larry Bates, May 25, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kurt Krueckeberg
    Replies:
    2
    Views:
    732
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    Nov 17, 2004
  2. Rick

    Comparing strings from within strings

    Rick, Oct 21, 2003, in forum: C Programming
    Replies:
    3
    Views:
    396
    Irrwahn Grausewitz
    Oct 21, 2003
  3. Klaus Neuner
    Replies:
    7
    Views:
    531
    Klaus Neuner
    Jul 26, 2004
  4. Girish Sahani
    Replies:
    17
    Views:
    587
    Boris Borcic
    Jun 9, 2006
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    814
    Malcolm
    Jun 24, 2006
Loading...

Share This Page