Judeah Wallace
Judeah Wallace

Reputation: 1

Printing coordinates in a certain range

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?

Upvotes: 0

Views: 272

Answers (2)

Nic3500
Nic3500

Reputation: 8621

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.

Upvotes: 1

wwii
wwii

Reputation: 23773

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

Upvotes: 0

Related Questions