passing multiple strings to string.find()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hokiegal99

    passing multiple strings to string.find()

    How do I say:

    x = string.find(fil es, 'this', 'that', 'the-other')

    currently I have to write it like this to make it work:

    x = string.find(fil es, 'this')
    y = string.find(fil es, 'that')
    z = string.find(fil es, 'the-other')

  • Raymond Hettinger

    #2
    Re: passing multiple strings to string.find()


    "hokiegal99 " <hokiegal99@vt. edu> wrote in message
    news:3F331A9B.9 [email protected].. .[color=blue]
    > How do I say:
    >
    > x = string.find(fil es, 'this', 'that', 'the-other')
    >
    > currently I have to write it like this to make it work:
    >
    > x = string.find(fil es, 'this')
    > y = string.find(fil es, 'that')
    > z = string.find(fil es, 'the-other')[/color]

    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


    Comment

    • Bengt Richter

      #3
      Re: passing multiple strings to string.find()

      On Thu, 07 Aug 2003 23:35:55 -0400, hokiegal99 <hokiegal99@vt. edu> wrote:
      [color=blue]
      >How do I say:
      >
      >x = string.find(fil es, 'this', 'that', 'the-other')
      >
      >currently I have to write it like this to make it work:
      >
      >x = string.find(fil es, 'this')
      >y = string.find(fil es, 'that')
      >z = string.find(fil es, 'the-other')
      >[/color]

      You might try the re module, e.g.,
      [color=blue][color=green][color=darkred]
      >>> import re
      >>> rxo = re.compile(r'th is|that|the-other')
      >>> pos = 0
      >>> while 1:[/color][/color][/color]
      ... 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

      Comment

      • François Pinard

        #4
        Re: passing multiple strings to string.find()

        [Fredrik Lundh]
        [color=blue]
        > Francois Pinard wrote:[/color]
        [color=blue][color=green]
        > > 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.[/color][/color]
        [color=blue]
        > 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.[/color]

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

        Comment

        Working...