Equivalent string.find method for a list of strings

2005-04-08 Thread jeremit0
I have read a text file using the command
lines = myfile.readlines()
and now I want to seach those lines for a particular string.  I was 
hoping there was a way to "find" that string in a similar way as 
searching simply a simple string.  I want to do something like

lines.find.('my particular string')
Is there a module that already exists that allows me to do this or will 
I have to created my own method?
Thanks,
Jeremy

--
http://mail.python.org/mailman/listinfo/python-list


Re: Equivalent string.find method for a list of strings

2005-04-08 Thread jeremit0
harold fellermann wrote:
file.readlines() returns a list of lines. You can either call find on each
element in this list, like:
for line in myfile.readlines() :
if line.find('my particular string') :
do_something()
I had thought that may be the only way to do it, I was hoping for 
something simpler as I wrote earlier.
Thanks for your help,
Jeremy

--
http://mail.python.org/mailman/listinfo/python-list


Re: Equivalent string.find method for a list of strings

2005-04-08 Thread jeremit0
Steve Holden wrote:
jeremit0 wrote:
harold fellermann wrote:
file.readlines() returns a list of lines. You can either call find on 
each
element in this list, like:

for line in myfile.readlines() :
if line.find('my particular string') :
do_something()
I had thought that may be the only way to do it, I was hoping for 
something simpler as I wrote earlier.
Thanks for your help,
Jeremy

If you want line numbers,. of course, then you can use
  for linenum, line in enumerate(myfile.readlines()):
  ...
Remember that true to Python's philosophy numbering will start at zero.
Is this any better than:
lines = myfile.readlines()
for linenum in xrange(len(lines)):
# Do stuff with lines[linenum] ...
?
Thanks,
Jeremy
--
http://mail.python.org/mailman/listinfo/python-list


Re: Equivalent string.find method for a list of strings

2005-04-08 Thread jeremit0
Steven Bethard wrote:
jeremit0 wrote:
Steve Holden wrote:
If you want line numbers,. of course, then you can use
  for linenum, line in enumerate(myfile.readlines()):
  ...
Remember that true to Python's philosophy numbering will start at zero.
Is this any better than:
lines = myfile.readlines()
for linenum in xrange(len(lines)):
# Do stuff with lines[linenum] ...

Well, if you use lines[linenum] often in that code, it'll be more 
efficient to use enumerate (or bind lines[linenum] to a name, which is 
basically what enumerate is doing for you).  Given the file test.py:

--
def elines(lines):
for linenum, line in enumerate(lines):
x = linenum
y = line
z = line
def xlines(lines):
for linenum in xrange(len(lines)):
x = linenum
y = lines[linenum]
z = lines[linenum]
--
Here's what I get with timeit:
[D:\Steve]$ python -m timeit -s"lines = range(1); import test" 
"test.elines(lines)"
100 loops, best of 3: 2.85 msec per loop

[D:\Steve]$ python -m timeit -s"lines = range(1); import test" 
"test.xlines(lines)"
100 loops, best of 3: 3.18 msec per loop

The other thing worth noting is that enumerate will work with any 
iterable, while your xrange technique won't.  Try:
for linenum, line in enumerate(myfile):
...
and
for linenum in xrange(len(myfile)):
line = myfile[linenum]
...
and see what kind of cool errors you get with the xrange solution. :)

STeVe
Wow that is significant evidence.  Thanks for doing that.
This is the first time I have posted to this list.  I really like it for 
two reasons.  1) There are lots of people who are willing to help out 
and 2) there are so many ideas now I can pick which one I like best.  I 
think I am leaning towards using enumerate.

Thanks,
Jeremy
--
http://mail.python.org/mailman/listinfo/python-list


Re: Equivalent string.find method for a list of strings

2005-04-12 Thread jeremit0
jeremit0 wrote:
I have read a text file using the command
lines = myfile.readlines()
and now I want to seach those lines for a particular string.  I was 
hoping there was a way to "find" that string in a similar way as 
searching simply a simple string.  I want to do something like

lines.find.('my particular string')
Is there a module that already exists that allows me to do this or will 
I have to created my own method?
Thanks,
Jeremy

After all the many suggestions (there were many good ones) I found 
something even better in "Python in a Nutshell".  I should have found it 
earlier.  I can do the following:

a = lines.index('my favorite line\n')
and a is the index of lines where "my favorite line' is located.  I 
hadn't seen this earlier, but it suits my purposes perfectly.
Thanks for everyone's suggestions,
Jeremy

--
http://mail.python.org/mailman/listinfo/python-list