Checking a string against multiple matches

Chris cwitts at gmail.com
Tue Dec 2 07:09:44 EST 2008


On Dec 2, 3:01 am, alex23 <wuwe... at gmail.com> wrote:
> On Dec 2, 5:31 am, Aaron Scott <aaron.hildebra... at gmail.com> 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



More information about the Python-list mailing list