Is there a way to use Python for loop more efficiently?

Asked 2 years ago, Updated 2 years ago, 13 views

    with open('Identified Atoms', 'w') as fo:
        for num, i in enumerate(iden_interfaceAtomA_B):                                                                                      
            print(f' >>> {num}')
            for j in iden_interfaceAtomC_D:                                                                                                    
                fo.write( str(i[0]) )
                fo.write( str(i[1]) )
                fo.write('\n')

There is a code that uses nested for loop in the code above, but it takes too long as the wearable object (?) gets bigger. I want to make a code more pythonic and efficiently, so what should I do?

python

2022-09-20 18:59

1 Answers

    iden_interfaceAtomC_D_len = len(iden_interfaceAtomC_D)

    with open('Identified Atoms', 'w') as fo:
        for num, i in enumerate(iden_interfaceAtomA_B):                                                                                      
            print(f' >>> {num}')
            #for j in iden_interfaceAtomC_D:                                                                                                    
            #    #    fo.write( str(i[0]) )
            #    #    fo.write( str(i[1]) )
            #    #    fo.write('\n')
            fo.write( (str(i[0])+str(i[1])+'\n') * iden_interfaceAtomC_D_len )


2022-09-20 18:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.