Equivalent string.find method for a list of strings

Steve Holden steve at holdenweb.com
Fri Apr 8 18:29:24 EDT 2005


jeremit0 wrote:
> 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] ...
> 
Only if, as I said, you want line numbers ...

regards
  Steve
-- 
Steve Holden        +1 703 861 4237  +1 800 494 3119
Holden Web LLC             http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/




More information about the Python-list mailing list