Home > Software engineering >  Printing coordinates in a certain range
Printing coordinates in a certain range

Time:07-11

I am currently running a code that prints coordinates if the conditions are met, it looks like this and runs completely fine.

y=[]
with open('insert my top secret super file here') as protein:
      for i in protein:
          i=i.split()
          coords=[]
          if i[0]=="ATOM" and i[2]=="CA" and i[4]=="A":
              coords.append(float(i[6]))
              coords.append(float(i[7]))
              coords.append(float(i[8]))
              y.append(coords)
 print (y)

I now want to print the coordinates if i[5] is in range 1–288, but when I add it to the line like this:

if i[0]=="ATOM" and i[2]=="CA" and i[4]=="A" and i[5] is in range(1,288):

The only thing that prints out is:

[]

What am I doing wrong?

CodePudding user response:

Remove the is.

The syntax is:

if i[0]=="ATOM" and i[2]=="CA" and i[4]=="A" and i[5] in range(1,289):

And @Pwuurple's comment is spot on, you probably have to use int(i[5]) as well.

note: I tested this on python3.

CodePudding user response:

Specify a lower and upper bound with a chained comparison.

if i[0]=="ATOM" and i[2]=="CA" and i[4]=="A" and (1 <= i[5] < 288):

6.10. Comparisons

  • Related