if 'hallo' in ['hallooo','halloooooooo'] problem

Discussion in 'Python' started by Robert, Apr 8, 2004.

  1. Robert

    Robert Guest

    Hi,

    I have a little problem and mybe one of you has got the solution.
    I would like to check if a string is in a list of strings.
    The following returns true:
    if 'hallo' in ['halloooo','hallooooooooo']:
    pass

    but I want to check if the string is exactly equal to any string in the
    list.
    How can I do this without the need to do something like this:
    def stringInList(self,string,list):
    for oneStr in list:
    if string == oneStr:
    return 1
    return 0


    Thank you in advance,

    Robert
     
    Robert, Apr 8, 2004
    #1
    1. Advertising

  2. Robert

    Peter Hansen Guest

    Robert wrote:

    > I have a little problem and mybe one of you has got the solution.
    > I would like to check if a string is in a list of strings.
    > The following returns true:
    > if 'hallo' in ['halloooo','hallooooooooo']:
    > pass


    c:\>python
    >>> 'hallo' in ['hallooo', 'halllloooo']

    False


    Given that "if" is not an expression, and so "returns" nothing
    at all, please explain what you mean above... clearly the
    "in" expression is behaving as you requested...

    -Peter
     
    Peter Hansen, Apr 8, 2004
    #2
    1. Advertising

  3. Robert

    Jeff Epler Guest

    Does it really work the way you describe?

    2.2:
    >>> 'xy' in ['xyz']

    0
    >>> 'xy' in ['xy', 'xyz']

    1

    2.3:
    >>> 'xy' in ['xyz']

    False
    >>> 'xy' in ['xy', 'xyz']

    True

    2.4 CVS:
    >>> 'xy' in ['xyz']

    False
    >>> 'xy' in ['xy', 'xyz']

    True

    The meaning that has changed is "s1 in s2" when s1 is a string not of
    length 1, and s2 is a string.

    2.2:
    >>> 'xy' in 'xyz'

    TypeError: 'in <string>' requires character as left operand

    2.3:
    >>> 'xy' in 'xyz'

    True

    2.4 CVS:
    >>> 'xy' in 'xyz'

    True

    Jeff
     
    Jeff Epler, Apr 8, 2004
    #3
  4. Robert

    Vrai Stacey Guest

    Robert wrote:
    > I have a little problem and mybe one of you has got the solution.
    > I would like to check if a string is in a list of strings.
    > The following returns true:
    > if 'hallo' in ['halloooo','hallooooooooo']:
    > pass


    What version of Python are you using? With 2.2.3 I get the behavior you
    want using your first example.

    So ...
    > if 'hallo' in ['halloooo','hallooooooooo']: print "Hallo!";
    > if 'hallo' in ['halloooo','hallo' ]: print "Hallo!";

    Hallo!

    vrai.
     
    Vrai Stacey, Apr 8, 2004
    #4
  5. Robert

    Robert Guest

    Sorry!!!!

    the example I gave I did not check. Of course it works.

    The problem is somewhere else. My programm was doing this:

    if 'hallo' in 'hallooooo':
    .....

    instead of (what I was thinking):

    if 'hallo' in ['hallo']:
    .....


    so , everything is fine. I did not see, that the variable holding the second
    string is a string instead of a list.


    Thanks for your replies!!!!!!



    "Robert" <> wrote in message
    news:c53k55$mm2$...
    > Hi,
    >
    > I have a little problem and mybe one of you has got the solution.
    > I would like to check if a string is in a list of strings.
    > The following returns true:
    > if 'hallo' in ['halloooo','hallooooooooo']:
    > pass
    >
    > but I want to check if the string is exactly equal to any string in the
    > list.
    > How can I do this without the need to do something like this:
    > def stringInList(self,string,list):
    > for oneStr in list:
    > if string == oneStr:
    > return 1
    > return 0
    >
    >
    > Thank you in advance,
    >
    > Robert
    >
    >
     
    Robert, Apr 8, 2004
    #5
    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. Andreas Suurkuusk
    Replies:
    0
    Views:
    4,031
    Andreas Suurkuusk
    Jul 27, 2003
  2. Ted Miller
    Replies:
    0
    Views:
    5,220
    Ted Miller
    Sep 13, 2003
  3. Merek
    Replies:
    0
    Views:
    2,001
    Merek
    Dec 3, 2003
  4. Hallo

    , Oct 3, 2008, in forum: C Programming
    Replies:
    15
    Views:
    766
    user923005
    Oct 6, 2008
  5. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    581
    Bullschmidt
    May 11, 2004
Loading...

Share This Page