new user needs help!

Steve Holden steve at holdenweb.com
Wed Apr 9 22:08:01 EDT 2008


drjekil wrote:
> I have done something so far about that problem,but its not the good way to
> do it
> 
> need ur comments about that....
> 
Well, at least you can see that your approach is not satisfactory, so 
that means you have some sense of what's good and bad programming/

> 
> from string import  *;
> import sys
> 
> myfile = open("/afs/pdc.kth.se/home/d/debnath/membrane/1a91A.txt")
> a = myfile.readlines()
> data = myfile.readlines()

The first readlines() exhausts the file.

> for line in myfile.readlines():

You should omit the assignments to a and to data, as they are using up 
the contents of the file that you want this statrement to iterate over.

>        fields = line.split('\t')
> 
>        items=fields.strip()
>        list1.append(items[1])
>
Note that what you are doing here is building a list of the lines in the 
file to iterate over later, when you could just iterate over the lines 
in the file.

> 
> for i in aminoacid:
>     if  10.0 <= z <= 22.0:
>     matches.append([1,i]) 
> #here i am getting comment!  bash-3.1$ python bb.py
>   File "bb.py", line 16
>     matches.append([1,i])
>           ^
> IndentationError: expected an indented block
>   
>     else:
>     matches.append([-1,i])
> 
The error message is literally correct. The statement(s) controlled by 
an if must be indented relative to the if statement itself - remember, 
Python uses indentation to indicate block structure.

Also, it seems a little unnecessary to build a list of all the matching 
lines and then process them, when you could process them one at a time 
as you read them from the file!

> print "#T/F  A  C  D  E  F  G  H  I  K  L  M  N  P  Q  R  S  T  V  W  X  Y 
> Z"
> 
That looks fine!

> for a in range(0,len(matches),1):
>      
>     if     matches[a][0]==1:
>    
>     if matches[a][1]=='A':
>         print "1  1:1 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0
> 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     elif matches[a][1]=='C':
>         print "1  1:0 2:1 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0
> 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     elif matches[a][1]=='D':
> 
>         print "    1  1:0 2:0 3:1 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
> """    if matches[a][1]=='E' and matches[a][0]==1:
> 
>         print "    1  1:0 2:0 3:0 4:1 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='F' and matches[a][0]==1:
> 
>         print "    1  1:0 2:0 3:0 4:0 5:1 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='G' and matches[a][0]==1:
> 
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:1 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='H' and matches[a][0]==1:
> 
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:1 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='I' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:1 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='K' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:1 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='L' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:1 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='M' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:1 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='N' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:1
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='P' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:1 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='Q' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:1 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='R' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:1 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='S' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:1 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='T' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:1 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='V' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:1 19:0 20:0 21:0"
>     if matches[a][1]=='X' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:1 20:0 21:0"
>     if matches[a][1]=='Y' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:1 21:0"
>     if matches[a][1]=='Z' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:1"
>    
> 
> """
>     else:
>     if matches[a][1]=='A':
>         print "-1  1:1 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0
> 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     elif matches[a][1]=='C':
>             print "-1  1:0 2:1 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     elif matches[a][1]=='D':
> 
>         print "    1  1:0 2:0 3:1 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
> """    if matches[a][1]=='E' and matches[a][0]==1:
> 
>         print "    1  1:0 2:0 3:0 4:1 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='F' and matches[a][0]==1:
> 
>         print "    1  1:0 2:0 3:0 4:0 5:1 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='G' and matches[a][0]==1:
> 
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:1 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='H' and matches[a][0]==1:
> 
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:1 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='I' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:1 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='K' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:1 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='L' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:1 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='M' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:1 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='N' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:1
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='P' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:1 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='Q' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:1 15:0 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='R' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:1 16:0 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='S' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:1 17:0 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='T' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:1 18:0 19:0 20:0 21:0"
>     if matches[a][1]=='V' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:1 19:0 20:0 21:0"
>     if matches[a][1]=='X' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:1 20:0 21:0"
>     if matches[a][1]=='Y' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:1 21:0"
>     if matches[a][1]=='Z' and matches[a][0]==1:
>        
>         print "    1  1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0
> 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:1"
>    waiting for ur opinion.
>    thanks
>         
I think you're right about the logic being unnecessarily complex :-)

The following is untested, but it will give you some idea of how you 
might proceed.

myfile = open("/afs/pdc.kth.se/home/d/debnath/membrane/1a91A.txt")
print "#T/F  A  C  D  E  F  G  H  I  K  L  M  N  P  Q  R  S  T  V  W  X 
  Y Z"
for line in myfile.readlines():
     fields = line.strip().split('\t')
     if 10.0 <= float(fields[6]) <= 22.0: # Sets T/F flag
         flag = 1
     else:
         flag = -1
     pos = "ACDEFGHIKLMNPQRSTVWXYZ".index(fields[1])
     m = []
     for i in range(20):
         if pos == i:
             m.append("%d:1")
         else:
             m.append("%d:0")
     print flag, " ".join(m)

This is not quite as compact as it could be. What you could do is try 
running it with print statements inserted to show you what value 
variables are taking in statements you don't understand.

Hope this helps!

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list