Checking a string against multiple matches

Discussion in 'Python' started by Aaron Scott, Dec 1, 2008.

  1. Aaron Scott

    Aaron Scott Guest

    I've been trying to read up on this, but I'm not sure what the
    simplest way to do it is.

    I have a list of string. I'd like to check to see if any of the
    strings in that list matches another string.

    Pseudocode:

    if "two" in ["one", "two", "three", "four"]:
    return True

    Is there any built-in iteration that would do such a thing, or do I
    have to write a function to check for me? I was using .index on the
    list, but it would return True for strings that contained the search
    string rather than match it exactly, leading to false positives in my
    code.
    Aaron Scott, Dec 1, 2008
    #1
    1. Advertising

  2. Aaron Scott

    Jerry Hill Guest

    On Mon, Dec 1, 2008 at 2:31 PM, Aaron Scott <> wrote:
    > Pseudocode:
    >
    > if "two" in ["one", "two", "three", "four"]:
    > return True


    That works, just like you wrote it:

    >>> "two" in ["one", "two", "three", "four"]

    True

    >>> "two" in ["one", "twofer", "three", "four"]

    False

    If that doesn't answer your question, maybe you could give some examples.

    --
    Jerry
    Jerry Hill, Dec 1, 2008
    #2
    1. Advertising

  3. Aaron Scott

    Peter Otten Guest

    Aaron Scott wrote:

    > I've been trying to read up on this, but I'm not sure what the
    > simplest way to do it is.
    >
    > I have a list of string. I'd like to check to see if any of the
    > strings in that list matches another string.
    >
    > Pseudocode:
    >
    > if "two" in ["one", "two", "three", "four"]:
    > return True


    Why /pseudo/ ?

    >>> if "two" in ["one", "two", "three", "four"]:

    .... print "match"
    .... else:
    .... print "no match"
    ....
    match
    >>> if "seven" in ["one", "two", "three", "four"]:

    .... print "match"
    .... else:
    .... print "no match"
    ....
    no match

    > Is there any built-in iteration that would do such a thing, or do I
    > have to write a function to check for me? I was using .index on the
    > list, but it would return True for strings that contained the search
    > string rather than match it exactly, leading to false positives in my
    > code.


    You didn't check carefully. list.index() gives you a value error when no
    matching item is found:

    >>> ["one", "two", "three", "four"].index("seven")

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: list.index(x): x not in list

    Peter
    Peter Otten, Dec 1, 2008
    #3
  4. Aaron Scott

    Aaron Scott Guest

    Damn you, Python, and your loose documentation! It never occurred to
    me to actually TRY my pseudocode, since I couldn't find anything on
    that type of statement. Anyway, feel free to ignore me from now on.
    Aaron Scott, Dec 1, 2008
    #4
  5. Aaron Scott

    Jerry Hill Guest

    On Mon, Dec 1, 2008 at 3:29 PM, Aaron Scott <> wrote:
    > Damn you, Python, and your loose documentation! It never occurred to
    > me to actually TRY my pseudocode, since I couldn't find anything on
    > that type of statement. Anyway, feel free to ignore me from now on.


    I'm not sure where you think the "in" operator should be documented.
    It's in the documentation for sequence types:
    http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange

    The tutorial points there too, in its discussion of strings.

    It's also in the language reference, in the section on comparision operators:
    http://docs.python.org/reference/expressions.html#id12

    Was there someplace you were looking that you expected to find
    something about the containment operator and couldn't find it? Maybe
    it would be worth adding a "See Also" someplace?

    --
    Jerry
    Jerry Hill, Dec 1, 2008
    #5
  6. Aaron Scott

    alex23 Guest

    On Dec 2, 5:31 am, Aaron Scott <> wrote:
    > I was using .index on the
    > list, but it would return True for strings that contained the search
    > string rather than match it exactly, leading to false positives in my
    > code.


    Are you sure? That doesn't seem like standard behaviour.

    >>> l = ["one", "two", "three", "four"]
    >>> l.index('on')

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: list.index(x): x not in list
    >>> l.index('thre')

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: list.index(x): x not in list

    The only time I'd expect it to do partial matches is if you were doing
    string.index(string), rather than list.index(string):

    >>> "four".index('our')

    1
    alex23, Dec 2, 2008
    #6
  7. Aaron Scott

    Chris Guest

    On Dec 2, 3:01 am, alex23 <> wrote:
    > On Dec 2, 5:31 am, Aaron Scott <> wrote:
    >
    > > I was using .index on the
    > > list, but it would return True for strings that contained the search
    > > string rather than match it exactly, leading to false positives in my
    > > code.

    >
    > Are you sure? That doesn't seem like standard behaviour.
    >
    > >>> l = ["one", "two", "three", "four"]
    > >>> l.index('on')

    >
    > Traceback (most recent call last):
    >   File "<stdin>", line 1, in <module>
    > ValueError: list.index(x): x not in list>>> l.index('thre')
    >
    > Traceback (most recent call last):
    >   File "<stdin>", line 1, in <module>
    > ValueError: list.index(x): x not in list
    >
    > The only time I'd expect it to do partial matches is if you were doing
    > string.index(string), rather than list.index(string):
    >
    > >>> "four".index('our')

    >
    > 1


    It would if the OP was iterating over the list and checking that item
    with .index so it uses the string.index instead of list.index
    Chris, Dec 2, 2008
    #7
  8. Aaron Scott

    alex23 Guest

    On Dec 2, 10:09 pm, Chris <> wrote:
    > On Dec 2, 3:01 am, alex23 <> wrote:
    > > The only time I'd expect it to do partial matches is if you were doing
    > > string.index(string), rather than list.index(string):


    > It would if the OP was iterating over the list and checking that item
    > with .index so it uses the string.index instead of list.index


    Which is what I was implying when I wrote "The only time I'd expect it
    to do partial matches is if you were doing string.index(string),
    rather than list.index(string)", oddly enough :)
    alex23, Dec 2, 2008
    #8
    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. tomasz
    Replies:
    7
    Views:
    344
    Jonathan Gardner
    Dec 18, 2007
  2. Chumley the Walrus

    Checking to see if a referring url/link matches

    Chumley the Walrus, Jul 1, 2004, in forum: ASP General
    Replies:
    4
    Views:
    140
    Steven Burn
    Jul 1, 2004
  3. Jochen Brenzlinger
    Replies:
    5
    Views:
    684
    Daniel Pitts
    Nov 21, 2011
  4. Rob Sanheim
    Replies:
    10
    Views:
    203
    Rob Sanheim
    Jan 14, 2007
  5. Alex DeCaria
    Replies:
    10
    Views:
    186
    Robert Klemme
    Apr 14, 2010
Loading...

Share This Page