[Tutor] improve the code
Dave Angel
d at davea.name
Tue Nov 1 15:33:30 CET 2011
On 11/01/2011 10:11 AM, lina wrote:
> Hi,
>
> The following code (luckily) partial achieved what I wanted, but I
> still have few questions:
>
>
> #!/usr/bin/python3
>
> import os.path
>
> INFILEEXT=".txt"
> OUTFILEEXT=".new"
> DICTIONARYFILE="dictionary.pdb"
> orig_dictionary={}
> new_dictionary={}
> abetaABresidues={}
>
> def processonefiledata(infilename):
> with open(infilename,"r") as f:
> for line in f:
> parts=line.strip().split()
> orig_dictionary[parts[0]]=parts[1]
>
>
> def build_abetadictionary(infilename,olddict):
> with open(infilename,"r") as f:
> for line in f:
> parts=line.strip().split()
> if parts[0] != "85CUR" and (parts[0] not in abetaABresidues.keys()):
> abetaABresidues[parts[0]]=0
> for residues, numbers in abetaABresidues.items():
> if residues in olddict.keys():
> new_dictionary[residues]=olddict[residues]
> else:
> new_dictionary[residues]=0
> with open(base+OUTFILEEXT,"w") as f:
> for residues, numbers in new_dictionary.items():
> print(residues,numbers,file=f)
> ## Q1: How can I sort the results, like the effect of | sort -g
>
> from something like:
> 84ALA 12
> :
> :
> 83ILE 28
> :
> :
>
> to
> :
> 83ILE 28
> 84ALA 12
> :
Just use the sort() method of the list object. In particular, items()
returns an unordered list, so it's ready to be sorted.
for residues, numbers in new_dictionary.items().sort():
That will sort such that residues are in sorted order.
--
DaveA
More information about the Tutor
mailing list