Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > passing multiple strings to string.find()

Reply
Thread Tools

passing multiple strings to string.find()

 
 
hokiegal99
Guest
Posts: n/a
 
      08-08-2003
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')

 
Reply With Quote
 
 
 
 
Raymond Hettinger
Guest
Posts: n/a
 
      08-08-2003

"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


 
Reply With Quote
 
 
 
 
Bengt Richter
Guest
Posts: n/a
 
      08-08-2003
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
 
Reply With Quote
 
=?iso-8859-1?q?Fran=E7ois_Pinard?=
Guest
Posts: n/a
 
      08-09-2003
[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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
How to generate k+1 length strings from a list of k length strings? Girish Sahani Python 17 06-09-2006 11:01 AM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57