beginner in python

Steve Holden steve at holdenweb.com
Thu Aug 2 00:54:29 EDT 2007


Beema shafreen wrote:
> hi everybody,
>    I am beginner in python
>  I have to calculate the euclidean distance between the atoms from a pdb 
> file
> i have written the the code and its shows me some error ,
> the code:
> import re
> import string
> import math
> ab =[]
> x_value = []
> y_value = []
> z_value = []
> fh = open("1K5N.pdb",'r')
> for atom in fh.readlines():
>    a = atom.strip()
>    pattern= re.compile('^ATOM.*')
>    atom_file= pattern.search(a)
>    if  atom_file:
>        atom_data = atom_file.group()
>        x_coordinate = atom_data[31:38]
>        y_coordinate = atom_data[39:46]
>        z_coordinate = atom_data[47:54]
>        x_value.append(x_coordinate)
>        y_value.append(y_coordinate)
>        z_value.append(z_coordinate)
> for x in range(len(x_value)):
>              x_co = float(x_value[x])-float(x_value[x+1])
>              y_co = float(y_value[x])-float(y_value[x+1])
>              z_co = float(z_value[x])-float(z_value[x+1])
>              data = math.sqrt(x_co)*(x_co)+(y_co)*(y_co)+(z_co)*(z_co)
>              print data
> ~
> and the error ,message
>  File "pdb_fetching.py", line 22, in ?
>     x_co = float(x_value[x])-float(x_value[x+1])
> IndexError: list index out of range
> 
> 
> can you suggest me the mistake i have made
> 
suppose you have an x_value list of length 6. Valid indexes go from 0 to 
5. Then x is going to start at 0 and go up to 5. The last time around 
the loop the expression "x_value[x+1]" is going to try and use 6 as an 
index, thus trying to address past the end of the list.

Since the data values are the RMS differences between successive points, 
there are only five differences for a six-element list.

Try using

     for x in range(len(x_value)-1):

instead.

By the way, you presented your question very well - all necessary 
information was there, and you didn't put in any mistaken guesses about 
what might be going wrong. Well done, and welcome to Python! You will 
find you can learn it very quickly.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list