passing multiple strings to string.find()

Raymond Hettinger vze4rx4y at verizon.net
Fri Aug 8 00:27:11 EDT 2003


"hokiegal99" <hokiegal99 at vt.edu> wrote in message
news:3F331A9B.9020907 at vt.edu...
> How do I say:
>
> x = string.find(files, 'this', 'that', 'the-other')
>
> currently I have to write it like this to make it work:
>
> x = string.find(files, 'this')
> y = string.find(files, 'that')
> z = string.find(files, 'the-other')

Try this:

   x, y, z = map(files.find, ['this', 'that', 'the-other'])

or, if you're just trying to find the first match:

  re.search('this|that|the-other', files).start()


OTOH, you've hinted at an application that may not
appropriate for multiple string searches.  Instead, look
at building a dictionary or list of files -- they are most
easily searched and better suited for associating other
data such as file sizes, etc.




Raymond Hettinger






More information about the Python-list mailing list