[SciPy-user] stupid python question : for in in range(nlines) :

Bill Dandreta wjdandreta at att.net
Thu Apr 20 13:39:20 CDT 2006


I do this kind of thing with for loops all the time. It usually takes 
this form:

This skips a line if if doesn't meet your processing condition:

for line in file('filename):
    parse line here
    if condition: continue
    process line here

If you need to look forward (or backward) some number of lines, use a 
second loop like this:

for i in range(nlines) :
    if condition: continue
    ...
    k=1
    while k<max_look_forward and i<nlines:
        ...
        k += 1
        if ...:
            set condition variable

Bill

Eric Emsellem wrote:

>Hi,
>
>I have a very dum python question here, hopefully someone can answer 
>this in no time:
>
>I have a script including lines such as (reading a file with "nlines" 
>lines):
>
>##############################
>for i in range(nlines) :
>    ...
>    ...
>    while i < nlines :
>      ....
>      i += 1
>      if ... :
>        break
>#############################
>
>But of course the "while" loop changes the increment "i", but then, when 
>the break condition is valid, it returns to the "for" loop and starts 
>again with the set of lines WITHOUT taking into account the fact that 
>"i" was incremented (so that it should not read these lines AGAIN).
>
>Hope this is clear. Let me know if you have a simple solution (my 
>scripting habits are coming from C, hence the way I stupily wrote things 
>here...)
>
>Thanks in advance for any help there
>
>Eric
>
>_______________________________________________
>SciPy-user mailing list
>SciPy-user at scipy.net
>http://www.scipy.net/mailman/listinfo/scipy-user
>
>  
>



More information about the SciPy-user mailing list