passing multiple strings to string.find()

Discussion in 'Python' started by hokiegal99, Aug 8, 2003.

  1. hokiegal99

    hokiegal99 Guest

    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')
     
    hokiegal99, Aug 8, 2003
    #1
    1. Advertising

  2. "hokiegal99" <> wrote in message
    news:...
    > 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
     
    Raymond Hettinger, Aug 8, 2003
    #2
    1. Advertising

  3. On Thu, 07 Aug 2003 23:35:55 -0400, hokiegal99 <> wrote:

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


    You might try the re module, e.g.,

    >>> import re
    >>> rxo = re.compile(r'this|that|the-other')
    >>> pos = 0
    >>> while 1:

    ... m = rxo.search(' Find this or the-other or that and this.', pos)
    ... if not m: break
    ... print '%4s: %s' % (m.start(), m.group())
    ... pos = m.end()
    ...
    6: this
    14: the-other
    27: that
    36: this

    If some search strings have a common prefix, you'll have to put
    the longest first in the regex, since re grabs the first match it sees.

    Regards,
    Bengt Richter
     
    Bengt Richter, Aug 8, 2003
    #3
  4. [Fredrik Lundh]

    > Francois Pinard wrote:


    > > Given the above,
    > >
    > > build_regexp(['this', 'that', 'the-other'])
    > >
    > > yields the string 'th(?:is|at|e\\-other)', which one may choose to
    > > `re.compile' before use.


    > the SRE compiler looks for common prefixes, so "th(?:is|at|e\\-other)" is
    > no different from "this|that|the-other" on the engine level.


    Thanks for the note. So the `build_regexp' function is not useful after
    all. It was indirectly written around a speed problem in the GNU regexp
    engine, but seemingly, the Python regexp engine knows better already. As I
    wrote earlier, I first saw Emacs Lisp `regexp-opt' used within `enscript'..

    A speed comparison between both methods shows that they are fairly
    equivalent. A small difference is that `build_regexp', given that one of
    the word is a prefix of another, automatically recognises the longest one,
    while a naive regexp of '|'.join(words) recognises whatever happens to be
    listed first. Of course, this is easily solved by sorting, then reversing
    the word list before producing the naive regexp.

    --
    Fran├žois Pinard http://www.iro.umontreal.ca/~pinard
     
    =?iso-8859-1?q?Fran=E7ois_Pinard?=, Aug 9, 2003
    #4
    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. Bart Nessux

    str.find for multiple strings

    Bart Nessux, Feb 11, 2004, in forum: Python
    Replies:
    7
    Views:
    476
    Peter Hansen
    Feb 12, 2004
  2. Vronans
    Replies:
    0
    Views:
    6,319
    Vronans
    Nov 20, 2006
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    810
    Malcolm
    Jun 24, 2006
  4. Replies:
    2
    Views:
    141
    Anno Siegel
    Feb 15, 2006
  5. Replies:
    5
    Views:
    324
    DJ Stunks
    Nov 20, 2006
Loading...

Share This Page