Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Looping on each line and each position in python

I’m stuck in a small looping problem.
I have a file with different positions.
p = [10, 11, 16]
I have a input (have 5 line for example) file like this:
s= BCCDDDCDCCDDDDDDABCDABCABDBACBDCAACCBBCABACBCCABCACBCDCCCBDBACDCBBCBCBCCCACADAACCABABADBCBAABBBCCBB
BCCDDDCDCDDDDCDDABCDABCABDBACBDDAACDBBCABACBCDBBCACBCCCCCBDBACDBBBCBCBACBACACAACCCBABADBCBAABBBCCBB
BCCDDDCDCDDDDCCDABCDABCABDBACBDDAADDBBCABACBCDBBCACBCCCCCBDBACDBBBCBCBACCACADAACCBBABADBCCACBBBCCBB
BCCDDDCDCCDDDCCDABCDABCDBDBACBDCAADDCBCABACBCCABCACBCDCCCBDBACDBBBCBCBBCCACADAACCBBABADBCCAAABBCCBB
BCCDDDCDCDDDDCCDABCDABCABDBACBDCAADDCBCABACBCCABCACBCCCCCBDBACDDBBCBCBACCACADAACCCBABADBCCAAABBCCBB

Now I like to make a loop in python which will print only those positions given in ‘p’ from each line in ‘s’
The desired output will be:
DDA
DDA
DDA
DDA
DDA

Now, this is my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

p=np.loadtxt('positions')
s=np.loadtxt('s')
for i in range (0, len(s)): 
    for i in p:  
        print(line [int(i)], end='')

output is : DDADDADDADDADDA
How do I convert this output to

DDA
DDA
DDA
DDA
DDA

>Solution :

you can try this:

p=np.loadtxt('positions')
s=np.loadtxt('s')
for i in range (0, len(s)): 
    for i in p:  
        print(line [int(i)], end='')
    print()

this way you add a linebreak at the end of the outer for-loop

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading