Velocity Reviews > Checking a string against multiple matches

# Checking a string against multiple matches

Aaron Scott
Guest
Posts: n/a

 12-01-2008
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.

Jerry Hill
Guest
Posts: n/a

 12-01-2008
On Mon, Dec 1, 2008 at 2:31 PM, Aaron Scott <(E-Mail Removed)> 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

Peter Otten
Guest
Posts: n/a

 12-01-2008
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

Aaron Scott
Guest
Posts: n/a

 12-01-2008
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.

Jerry Hill
Guest
Posts: n/a

 12-01-2008
On Mon, Dec 1, 2008 at 3:29 PM, Aaron Scott <(E-Mail Removed)> 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/stdty...-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

--
Jerry

alex23
Guest
Posts: n/a

 12-02-2008
On Dec 2, 5:31*am, Aaron Scott <(E-Mail Removed)> 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

Chris
Guest
Posts: n/a

 12-02-2008
On Dec 2, 3:01*am, alex23 <(E-Mail Removed)> wrote:
> On Dec 2, 5:31*am, Aaron Scott <(E-Mail Removed)> 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

alex23
Guest
Posts: n/a

 12-02-2008
On Dec 2, 10:09*pm, Chris <(E-Mail Removed)> wrote:
> On Dec 2, 3:01*am, alex23 <(E-Mail Removed)> 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

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Jochen Brenzlinger Java 5 11-21-2011 07:43 PM Alex DeCaria Ruby 10 04-14-2010 05:42 AM tomasz Python 7 12-18-2007 07:27 PM Rob Sanheim Ruby 10 01-14-2007 09:09 PM Chumley the Walrus ASP General 4 07-01-2004 10:27 PM