[Python] Different Starting Indices for Iterating over Large Files

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    I want to begin by saying I have NOT programmed in many, many years, so sorry if this is a somewhat trivial question. My interest has been mathematics for the last couple of years

    Here's my code:

    lastStop = False

    with open('K3.txt','r') as K3:
    with open('K4.txt','a') as K4:
    for tri0 in K3:
    tri = tri0.split(',')
    c=tri[2][:-1]
    c=int(c)
    a=tri[0]
    b=tri[1]
    a=int(a)
    b=int(b)
    for d in range(c+1,6821):
    if lastStop:
    if [adjMat[a][d],adjMat[d],adjMat[c][d]]==['1','1','1']:
    K4.write(str(a)+','+str(b)+','+str(c)+','+str(d)+'\n')
    elif str(a)+','+str(b)+','+str(c)+','+str(d)+'\n'=='670,3016,3426,5603\n':
    lastStop=True


    In this, I'm iterating over the lines of a very large file (K3.txt), doing some processing, and then writing to K4.txt. But, because it takes so long, I ran it over night last night. Because of that, I didn't get all the way through it, and the last line that I wrote to K4 was '670,3016,3426,5603\n'

    I'm planning on running this again tonight, wanting to start right after I get to the (a,b,c,d)=670,3016,3426,5603 point in the for loops, and my solution here doesn't feel elegant. I set a boolean "lastStop" to False, and then, once I get to that (a,b,c,d) value, I turn it to true so it starts running that first part of the if/elif statement

    I'm sure I'm losing a lot of time by having to go through all values up until that point

    I'm thinking there surely must be a way to start the "for tri0 in K3:" for loop at (a,b,c)=(670,3016,3426) point, but I'm not sure how to do that without looping over the row numbers, and Pythonista for some reason interprets K3 as a list sometimes and not a list other times

    For example, the a=tri[0] followed immediately by a=int(a) is because, for some reason, when I write "a=int(tri[0])", Pythonista throws me an error along the lines of "Object 'type' cannot be subindexed" (if you want the exact error, I can find out)

    I hope this made sense; thank you

    Continue reading...

Compartilhe esta Página