Checking a string against multiple matches

A

Aaron Scott

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.
 
P

Peter Otten

Aaron said:
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
 
A

Aaron Scott

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.
 
J

Jerry Hill

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

alex23

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):
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):
1
 
C

Chris

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):

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
 
A

alex23

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 :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top