[Tutor] improve the code

Dave Angel d at davea.name
Tue Nov 1 16:56:53 CET 2011


On 11/01/2011 11:11 AM, lina wrote:
> On Tue, Nov 1, 2011 at 10:33 PM, Dave Angel<d at davea.name>  wrote:
>> 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.
>
> Thanks, but still something went wrong here,
>
> Traceback (most recent call last):
>    File "fill-gap.py", line 41, in<module>
>      build_abetadictionary(DICTIONARYFILE,orig_dictionary)
>    File "fill-gap.py", line 31, in build_abetadictionary
>      for residues, numbers in new_dictionary.items().sort():
> AttributeError: 'dict_items' object has no attribute 'sort'
>

Peter fixed that one.  Actually my code wasn't even right in Python 2.x, 
as the sort() method sorts in place, and doesn't return a value.  His 
code will work in both 2.x and 3.x, and is thus a much better answer.  I 
frequently confuse the sort method and the sorted function, and judging 
from this list, so do many others.  I'm pretty good at spotting that 
error if someone else makes it ;-)


> I have another concerns,
> is it possible to append the output file content as a sing one,
> such as a.new is
> A 1
> B 3
>
> b.new is
> A 3
> B 5
>
> I wish the final one like:
>
> A 1 3
> B 3 5
>
> I will think about it. Thanks,


No idea how you came up with a.new or b.new.  Or even what they contain. 
  When you say a.new is "> A 1\n> B 3\n"  I've got to guess you're 
misrepresenting it.

But if you have two dictionaries that use EXACTLY the same keys, and you 
want to print out approximately that way, consider [untested]

def  printme(dict1, dict2):
     for key in sorted(dict1.keys()):
          print(key, dict1[key], dict2[key])


But notice that it won't print any value which has a key in dict2, but 
not in dict1.  And it'll get an exception if there's a key in dict1 
which is not in dict2.

So what's your real problem?  There are better ways to accomodate 
multiple sets of related data, and my answer doesn't help if there are 
3, or 4, or 42 dictionaries.

By the way, it's usually better to separate outputting the data from 
inputting.  Factoring code into separate functions makes the code more 
flexible when requirements change.

-- 

DaveA


More information about the Tutor mailing list